Make WordPress Core

Opened 2 weeks ago

Last modified 4 hours ago

#62523 new defect (bug)

Can't register Block Templates for CPTs with an underscore "_" character in the key

Reported by: alexandrebuffet's profile alexandrebuffet Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.7
Component: Editor Keywords: has-patch needs-testing has-testing-info
Focuses: template Cc:

Description

After several days of using the new Block Templates API, I’ve noticed that we can’t register a template for a CPT that has underscore "_" in its key, it displays the error "Template names must contain a namespace prefix. Example: my-pluginmy-custom-template".

For example, I have a plugin that declares a CPT "school_course" and if I declare its associated templates "my-pluginarchive-school_course" and/or "my-pluginsingle-school_course" I get the error.

This error seems to be displayed because the test on the template name via the RegEx "/[a-z0-9-]+\/\/[a-z0-9-]+$/" returns false in register method from WP_Block_Templates_Registry class.

Related issue in Gutenberg: https://github.com/WordPress/gutenberg/issues/67066

Attachments (1)

62523.patch (858 bytes) - added by karthickmurugan 2 weeks ago.
Allow underscore in register_block_template function patch.

Download all attachments as: .zip

Change History (8)

@karthickmurugan
2 weeks ago

Allow underscore in register_block_template function patch.

#1 @karthickmurugan
2 weeks ago

  • Keywords has-patch needs-testing added; needs-patch removed

Hello all.,

The above patch fixes the underscore issue in register_block_type function. Please have a look at this video for clear explanation and testing before and after the fix.

https://share.cleanshot.com/qknmwCwGjX5tBWbCdFX5

#2 @alexandrebuffet
2 weeks ago

Thanks @karthickmurugan, I tested your patch and it corrects the problem.

#3 @ankitkumarshah
12 days ago

  • Keywords has-testing-info added

Test Report

Description

This report validates whether the indicated patch works as expected.

Patch tested: https://core.trac.wordpress.org/attachment/ticket/62523/62523.patch

Environment

  • WordPress: 6.7.1
  • PHP: 8.1.29
  • Server: nginx/1.16.0
  • Database: mysqli (Server: 8.0.16 / Client: mysqlnd 8.1.29)
  • Browser: Chrome 131.0.0.0
  • OS: macOS
  • Theme: Twenty Twenty-Five 1.0
  • MU Plugins: None activated
  • Plugins:
    • Test Reports 1.2.0

Actual Results

  1. ✅ Issue resolved with patch.

Supplemental Artifacts

Before Patch

https://utfs.io/f/TnWMEUzoUd85e63Opo59in4IP8W2ScxrkUYbqyg7DsGdaTzp

After Patch

https://utfs.io/f/TnWMEUzoUd85ETTGOEgxzLduKa2yRBnh7lb60Gwq5Sgc9H8V

This ticket was mentioned in Slack in #core-test by oglekler. View the logs.


12 days ago

#5 @oglekler
12 days ago

  • Severity changed from major to normal

@noisysocks can you please look at this one?

This ticket was discussed during the Test team triage session.

According to WPCS
"Files should be named descriptively using lowercase letters. Hyphens should separate words."
https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/

From this, it looks like all underscores in post type names should be filtered to hyphens, but if we don't have it yet, it can make braking change, so, it looks like the best option is to allow underscores in the template names.

#6 @karthickmurugan
13 hours ago

@poena - Can you please take a look at this patch and move forward. Thanks in advance.

#7 @gaambo
4 hours ago

register_post_type uses sanitize_key to sanitize the post type key, which in turn uses the following regex pattern: /[^a-z0-9_\-]/ I would recommend, to use the same regex and also comment/document that this must be the same as in register_post_type/sanitize_key so that it works for all post types.
I don't think there should be any problems with file names, since the same works for existing .php templates in classic themes as well.

Note: See TracTickets for help on using tickets.