Open wpmark opened 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?
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?
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.
Ok thanks @Aljullu, I can make the PR to correct this, should I create it here or on the core?
@wpmark , @alexandrebuffet - I have add a patch file in the Core Trac with the fix and testing video.
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:
However if the post type is prefixed with something, in this case
hd_
them the this fails. For example the following doesn't work.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.