#63808 closed defect (bug) (fixed)
Block hooks are not applied to plugin-registered templates using `register_block_template()`
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 6.9 | Priority: | normal |
| Severity: | normal | Version: | 6.9 |
| Component: | Editor | Keywords: | 2nd-opinion has-patch |
| Focuses: | Cc: |
Description
Block hooks are not being applied to templates registered by plugins using the Template Registration API (register_block_template()). This creates inconsistent behavior where block hooks work for theme templates and user-created templates, but not for plugin-registered templates.
Originally issue reported in GB: https://github.com/WordPress/gutenberg/issues/71139
Step-by-step reproduction instructions
- Create a plugin.
- Add this snippet to register a template and hook a block:
<?php register_block_template( 'my-plugin//plugin-template', array( 'title' => 'Test Template', 'description' => 'This is a test template', 'content' => '<!-- wp:separator {"className":"is-style-dots"} --><hr class="wp-block-separator has-alpha-channel-opacity is-style-dots"/><!-- /wp:separator -->', ) ); add_action( 'single_template_hierarchy', function () { return array( 'plugin-template' ); } ); add_filter( 'hooked_block_types', function ( $hooked_block_types, $relative_position, $anchor_block_type, $context ) { if ( 'after' === $relative_position && 'core/separator' === $anchor_block_type ) { $hooked_block_types[] = 'core/post-title'; } return $hooked_block_types; }, 10, 4 );
- Create a new page and add a Separator block
- Visit a page (using theme template) and a post (using plugin template)
- Notice the page correctly applies the block hook, while the post doesn't
Expected Result: Block hooks should be applied consistently to all template sources (theme, user-created, and plugin-registered).
Actual Result: Plugin-registered templates do not have block hooks applied, while theme templates and user-created templates do.
Change History (5)
This ticket was mentioned in PR #9418 on WordPress/wordpress-develop by @iamadisingh.
7 months ago
#1
- Keywords has-patch added; needs-patch removed
@iamadisingh commented on PR #9418:
7 months ago
#2
Thanks for working on this, @Adi-ty! I tested with the testing steps from WordPress/gutenberg#71139 and I can confirm that:
- In the frontend, the template from the plugin correctly displays the hooks:
- When making a request to
/wp-json/wp/v2/templates/twentytwentyfive//plugin-template, the template in the response now has the hooks applied to the content.
- That is not a problem when accessing the template in the site editor (ie:
/wp-admin/site-editor.php?canvas=edit&p=%2Fwp_template%2Ftwentytwentyfive%2F%2Fplugin-template). Hooks are still rendered only once.I'm approving from my side, but I don't have permisions in WP core. @ockham wondering if you (or somebody you know) can help us move this forward?
Thanks for testing and confirming the changes. Hopefully we can get this moved forward soon.
@Bernhard Reiter commented on PR #9418:
7 months ago
#3
Thank you both! I'll go ahead and commit this 👍




This PR fixes an inconsistency where block hooks were not being applied to templates registered by plugins using
register_block_template().Trac ticket: https://core.trac.wordpress.org/ticket/63808