wp_register_alignment_support()


WordPressの関数wp_register_alignment_support()は、ブロックエディタで配置オプションを登録するために使用されます。

構文

wp_register_alignment_support( string $block_type );
  • $block_type(string)—配置サポートを追加するブロックタイプ。

例1:コアブロックに配置サポートを追加

基本的な例。コアの段落ブロックに配置サポートを追加します。

add_action('init',function(){wp_register_alignment_support('core/paragraph');});

例2:カスタムブロックに配置サポートを追加

カスタムブロックタイプに配置オプションを登録します。

add_action('init',function(){wp_register_alignment_support('my-plugin/custom-block');});

例3:複数のブロックに一括登録

複数のブロックタイプを配列でループ処理します。

add_action('init',function(){$blocks=['core/heading','core/image'];foreach($blocks as $block){wp_register_alignment_support($block);}});

例4:条件付きでサポートを追加

特定の条件下でのみ配置サポートを有効にします。

add_action('init',function(){if(current_theme_supports('align-wide')){wp_register_alignment_support('core/group');}});

例5:カスタム属性と併用

ブロック属性と組み合わせて使用します。

add_action('init',function(){register_block_type('my-plugin/block',['attributes'=>['align'=>['type'=>'string']]]);wp_register_alignment_support('my-plugin/block');});

例6:フック優先度の変更

initフックの優先度を変更して実行タイミングを調整。

add_action('init',function(){wp_register_alignment_support('core/columns');},20);

例7:プラグイン内での実装

プラグイン内で安全に機能を追加します。

class My_Plugin{public function init(){wp_register_alignment_support('core/verse');}}add_action('init',[new My_Plugin(),'init']);

注意点

  • この関数はブロックエディタが有効な場合のみ動作します
  • ブロックタイプは先に登録されている必要があります
  • テーマがadd_theme_support('align-wide')を宣言している場合、追加の幅オプションが有効になります

関連機能: