Make WordPress Core

Opened 7 months ago

Closed 7 months ago

Last modified 6 months ago

#63808 closed defect (bug) (fixed)

Block hooks are not applied to plugin-registered templates using `register_block_template()`

Reported by: iamadisingh's profile iamadisingh Owned by: bernhard-reiter's profile Bernhard Reiter
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

  1. Create a plugin.
  2. 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
);
  1. Create a new page and add a Separator block
  2. Visit a page (using theme template) and a post (using plugin template)
  3. 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

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

@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:

  1. In the frontend, the template from the plugin correctly displays the hooks:

Before After
https://private-user-images.githubusercontent.com/3616980/478382132-655e984a-80ca-43af-b328-e516fbf35556.png https://private-user-images.githubusercontent.com/3616980/478382098-47aa1d27-4159-43e5-b153-d43d9e9db4de.png

  1. 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.

Before After
https://private-user-images.githubusercontent.com/3616980/478382551-4fe1ce07-1ee0-4bd7-a8cf-12388131a8d4.png https://private-user-images.githubusercontent.com/3616980/478382523-2eb4c8b2-51ea-4c9d-aa8a-b4a027b2d6ee.png

  1. 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 👍

#4 @Bernhard Reiter
7 months ago

  • Owner set to Bernhard Reiter
  • Resolution set to fixed
  • Status changed from new to closed

In 60688:

Block Hooks: Apply block hooks to plugin-registered templates.

As of WordPress 6.7.0, it is possible -- e.g. for plugins -- to register block templates via a new API, register_block_template(). Unlike block templates loaded from theme files or from the database, however, those block templates didn't have Block Hooks applied. This changeset rectifies this inconsistency.

Props iamadisingh, aljullu, bernhard-reiter.
Fixes #63808.

#5 @sabernhardt
6 months ago

  • Milestone changed from Awaiting Review to 6.9
Note: See TracTickets for help on using tickets.