フック
ページ、パターン、サイトテンプレート、HTMLパーサーをカスタマイズするために使用できるフィルターとアクション。
パーサー
blockstudio/block_tags/builders
ブロックタグと要素マッピングされたカスタムブロックで使用する、ブロック配列ビルダーを登録します。各ブロックのコールバックには、属性配列と生の内部コンテンツが渡されます。
add_filter( 'blockstudio/block_tags/builders', function( $builders, $parser ) {
$builders['acf/hero'] = function( array $attrs, string $inner_content ) {
$inner_blocks = Blockstudio\Block_Tags::parse_inner_blocks( $inner_content );
return array(
'blockName' => 'acf/hero',
'attrs' => $attrs,
'innerBlocks' => $inner_blocks,
'innerHTML' => '',
'innerContent' => array_fill( 0, count( $inner_blocks ), null ),
);
};
return $builders;
}, 10, 2 );
blockstudio/block_tags/renderersとblockstudio/parser/renderersは同じレジストリを受け取ります。blockstudio/block_tags/buildersの後にこの順で実行され、同じブロックに対する先行のビルダーを置き換えられます。
blockstudio/parser/element_mapping
要素からブロックへのデフォルトのマッピングを上書きしますHTML。デフォルトでは、<h1>、<p>、<img>などの標準HTML要素はコアWordPressブロックにマッピングされます。このフィルターを使うと、任意の要素を別のブロックに割り当てられます。
add_filter( 'blockstudio/parser/element_mapping', function( $mapping ) {
$mapping['h1'] = 'custom/heading';
$mapping['h2'] = 'custom/heading';
$mapping['p'] = 'custom/paragraph';
$mapping['img'] = 'custom/image';
return $mapping;
}, 10, 2 );
<p>がcontentのリッチテキスト属性を持つ登録済みの非コアBlockstudioブロックにマッピングされる場合、その内部HTMLはcodeやimgなど、同様に要素マッピングされたインラインタグを含めて、その属性にコピーされます。子要素が画像だけの段落は、<img>がマッピングされるブロックになります。ネストされたブロックタグやブロックレベルのHTMLでは、通常どおり内部ブロック解析が行われます。
ページ
blockstudio/pages/paths
ページテンプレートの検索対象ディレクトリをフィルターします。
add_filter( 'blockstudio/pages/paths', function( $paths ) {
$paths[] = get_template_directory() . '/custom-pages';
$paths[] = MY_PLUGIN_DIR . '/pages';
return $paths;
} );
blockstudio/pages/manifest_scan_interval
アップグレード時に永続化されたコレクションのランタイム設定の初期値として使える、7.6.2より前のレガシーマニフェストキャッシュの最大経過時間をフィルターします。通常のリクエストでは永続化されたコレクション設定を使い、ページルートを定期的に再スキャンしません。コレクションマニフェストを変更した後は、wp bs pages syncを実行するか、Blockstudio\Pages::reconcile()を呼び出してください。
add_filter( 'blockstudio/pages/manifest_scan_interval', function() {
return 10;
} );
blockstudio/pages/create_post_data
新しいページを作成する前に投稿データをフィルターします。
add_filter( 'blockstudio/pages/create_post_data', function( $post_data, $page_data ) {
$post_data['post_author'] = 1;
return $post_data;
}, 10, 2 );
blockstudio/pages/update_post_data
既存のページを更新する前に投稿データをフィルターします。
add_filter( 'blockstudio/pages/update_post_data', function( $post_data, $post, $page_data ) {
return $post_data;
}, 10, 3 );
blockstudio/pages/synced
すべてのページの同期後に実行されるアクション。
add_action( 'blockstudio/pages/synced', function( $registry ) {
// $registry is the Page_Registry instance
} );
blockstudio/pages/post_created
ページ投稿の作成後に実行されるアクション。
add_action( 'blockstudio/pages/post_created', function( $post_id, $page_data ) {
// Do something after page creation
}, 10, 2 );
blockstudio/pages/post_updated
ページ投稿の更新後に実行されるアクション。
add_action( 'blockstudio/pages/post_updated', function( $post_id, $page_data ) {
// Do something after page update
}, 10, 2 );
パターン
blockstudio/patterns/paths
パターンテンプレートの検索対象ディレクトリをフィルターします。
add_filter( 'blockstudio/patterns/paths', function( $paths ) {
$paths[] = get_stylesheet_directory() . '/custom-patterns';
$paths[] = MY_PLUGIN_PATH . '/patterns';
return $paths;
} );
blockstudio/patterns/registered
すべてのパターンの登録後に実行されるアクション。
add_action( 'blockstudio/patterns/registered', function( $registry ) {
// $registry is the Pattern_Registry instance
} );
サイトテンプレート
blockstudio/site_templates/template_paths
template.jsonファイルの検索対象ディレクトリをフィルターします。
add_filter( 'blockstudio/site_templates/template_paths', function( $paths ) {
$paths[] = get_stylesheet_directory() . '/site-templates';
return $paths;
} );
blockstudio/site_templates/part_paths
part.jsonファイルの検索対象ディレクトリをフィルターします。
add_filter( 'blockstudio/site_templates/part_paths', function( $paths ) {
$paths[] = get_stylesheet_directory() . '/site-parts';
return $paths;
} );
blockstudio/site_templates/paths
2つの検出パスリストをまとめてフィルターします。値にはtemplates配列とparts配列が含まれます。
add_filter( 'blockstudio/site_templates/paths', function( $paths ) {
return $paths;
} );
blockstudio/site_templates/template_candidates
マニフェストと論理ソースツリーの解決後に、ソースファイルの候補をフィルターします。
add_filter(
'blockstudio/site_templates/template_candidates',
function( $candidates, $directory, $manifest ) {
$candidates[] = $directory . '/template.custom.php';
return $candidates;
},
10,
3
);
blockstudio/site_templates/templates
レジストリAPIが返す完全なテンプレートをフィルターします。
add_filter( 'blockstudio/site_templates/templates', function( $templates ) {
return $templates;
} );
blockstudio/site_templates/parts
レジストリAPIが返すテンプレートパーツをフィルターします。
add_filter( 'blockstudio/site_templates/parts', function( $parts ) {
return $parts;
} );
blockstudio/site_templates/template_content
Blockstudioがテンプレートソース文字列を解析してブロックに変換する前にフィルターします。
add_filter( 'blockstudio/site_templates/template_content', function( $content, $template ) {
return $content;
}, 10, 2 );
blockstudio/site_templates/part_content
Blockstudioがテンプレートパーツのソース文字列を解析してブロックに変換する前にフィルターします。
add_filter( 'blockstudio/site_templates/part_content', function( $content, $part ) {
return $content;
}, 10, 2 );
blockstudio/site_templates/parser
コンパイル済みテンプレートまたはパーツごとに使用するBlockstudio\Html_Parserインスタンスをフィルターします。Html_Parserを返します。それ以外の値の場合はデフォルトのパーサーが使われます。
add_filter(
'blockstudio/site_templates/parser',
function( $parser, $item ) {
return $parser;
},
10,
2
);
blockstudio/site_templates/discovered
コールド時の検出とコンパイルの後、再構築されたレジストリが保存される前に実行されるアクション。
add_action( 'blockstudio/site_templates/discovered', function( $registry ) {
// Inspect the rebuilt Site_Template_Registry.
} );
blockstudio/site_templates/registered
ファイルベースのSite Editorテンプレートレジストリの再構築と保存の後に実行されるアクション。
add_action( 'blockstudio/site_templates/registered', function( $registry ) {
// $registry is the Site_Template_Registry instance
} );
レジストリキャッシュがウォーム状態でヒットした場合、discoveredとregisteredのアクションは実行されません。