Skip to content

Block hooks are not applied to templates loaded via the Template registration API #71139

@Aljullu

Description

@Aljullu

Description

Apparently, block hooks are not being applied to templates registered by plugins using the Template registration API.

Step-by-step reproduction instructions

  1. Install the Dynamic Copyright Date plugin, to have a block as an example.
  2. Add this snippet to your site that (1) creates a template for posts, where only the Separator block is rendered and (2) hooks the Dynamic Copyright Date block to the Separator block:
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[] = 'dynamic-copyright-date/copyright-date-block';
		}
		return $hooked_block_types;
	},
	10,
	4
);
  1. Create a new page and add the Separator block.
  2. Now, in the frontend, visit a page (using the theme template) and a post (using the plugin template):
  3. Notice the page correctly applies the block hook (adds the Dynamic Copyright Date block after the Separator block), while the post, doesn't.

Screenshots, screen recording, code snippet

Page Post
Image Image

Environment info

  • WordPress 6.8.2.
  • Plugins: Code Snippets and Dynamic Copyright Date Block.
  • Themes used for testing: TT4 and TT5.

Please confirm that you have searched existing issues in the repo.

  • Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

  • Yes

Please confirm which theme type you used for testing.

  • Block
  • Classic
  • Hybrid (e.g. classic with theme.json)
  • Not sure

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Feature] Block hooks[Feature] Templates APIRelated to API powering block template functionality in the Site Editor[Type] BugAn existing feature does not function as intended

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions