WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.53k stars 4.21k forks source link

Register block template function fails in custom post type is prefixed #67066

Open wpmark opened 4 days ago

wpmark commented 4 days ago

Description

When using the register_block_template function to register block templates, if the post type name you are registering a template for includes a prefix the registration doesn't work.

For example, the following declaration works:

register_block_template( 'hd-people//single-person', [
    'title'       => __( 'Single Person', 'hd-people' ),
    'description' => __( 'Displays a single person profile.', 'hd-people' ),
    'content'     => hd_people_get_template_content( 'single-person.php' )
] );

However if the post type is prefixed with something, in this case hd_ them the this fails. For example the following doesn't work.

register_block_template( 'hd-people//single-hd_person', [
    'title'       => __( 'Single Person', 'hd-people' ),
    'description' => __( 'Displays a single person profile.', 'hd-people' ),
    'content'     => hd_people_get_template_content( 'single-hd_person.php' )
] );

Tagging @ndiego as requested.

Step-by-step reproduction instructions

Create a custom post type with a prefix for its name/slug.

Register a block template using the register_post_template() function for the single post template.

The template never appears in the site editor under templates.

Screenshots, screen recording, code snippet

No response

Environment info

WordPress 6.7 with the Twenty Twenty Five Theme. No other plugins installed or active.

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

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

Please confirm which theme type you used for testing.

ndiego commented 4 days ago

Thanks @wpmark. This issue is related to https://github.com/WordPress/gutenberg/pull/61577 and I have confirmed this bug. @Aljullu do you have any insights here?

alexandrebuffet commented 1 day ago

I've tried this new API with custom post types and I also use the underscore character “_” in their key so I get the same error.

This error seems to be displayed because one of statement for the template name check returns false in register method from WP_Block_Templates_Registry class:

elseif ( ! preg_match( '/^[a-z0-9-]+\/\/[a-z0-9-]+$/', $template_name ) ) {
    $error_message = __( 'Template names must contain a namespace prefix. Example: my-plugin//my-custom-template' );
    $error_code    = 'template_no_prefix';
} 

Updating the RegEx to allow the underscore character “_” should correct this problem?

Aljullu commented 1 day ago

Yes, we used the same RegExp that was used in the block registry. I don't have a strong opinion on this, but based on your comments it seems like a legit use case, so I agree on allowing the underscore character.

alexandrebuffet commented 16 hours ago

Ok thanks @Aljullu, I can make the PR to correct this, should I create it here or on the core?

karthick-murugan commented 11 hours ago

@wpmark , @alexandrebuffet - I have add a patch file in the Core Trac with the fix and testing video.