Replies: 3 comments 7 replies
|
Sorry for the delay in replying. Adding a |
|
To register a block template part from a plugin, you can use the You can do it like this: add_action( 'init', function() {
register_block_template_part(
'my-plugin/header',
array(
'title' => 'Plugin Header',
'description' => 'Header template part added by my plugin.',
'area' => 'header',
'content' => '<!-- wp:paragraph --><p>This is my custom header</p><!-- /wp:paragraph -->',
)
);
});Make sure:
|
|
Just to clarify the answer above before someone copies it into production: there is no Working approaches from a plugin today: 1. Register a block pattern scoped to a template-part area — closest first-party route. The pattern shows up in the Site Editor under "Add → Template part" for the matching area: add_action( 'init', function () {
register_block_pattern( 'my-plugin/header', [
'title' => __( 'My Plugin Header', 'my-plugin' ),
'content' => '<!-- wp:group --><div class="wp-block-group">…</div><!-- /wp:group -->',
'blockTypes' => [ 'core/template-part/header' ],
'templateTypes' => [ 'header' ],
] );
} );The user still instantiates the part themselves, but your plugin owns the content. 2. Extend add_filter( 'default_wp_template_part_areas', function ( $areas ) {
$areas[] = [
'area' => 'aside',
'label' => __( 'Aside', 'my-plugin' ),
'description' => __( 'Sidebar template parts.', 'my-plugin' ),
'icon' => 'aside',
'area_tag' => 'aside',
];
return $areas;
} );(Filter is in 3. Ship a companion block theme if you really need plugin-owned Worth a +1 on the existing tracking issue under the |
Uh oh!
There was an error while loading. Please reload this page.
Since WordPress 6.7, there's a new
register_block_templateAPI which can be used by plugins to register a custom block template.But how can we add a block template part from plugins?
All reactions