WordPress.org

Make WordPress Core

Opened 2 years ago

Last modified 2 years ago

#41518 new defect (bug)

The full-path body class is missing a hyphen.

Reported by: isaaclubow Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9
Component: Posts, Post Types Keywords: close
Focuses: template Cc:
PR Number:

Description

In a case where page templates (eg "full-width.php") are in a folder called "templates" - a body class is created called "page-template-templatesfull-width-php". This is because post-template.php line 611 sanitizes the file path without replacing "/" with a hyphen like it does for template name parts on line 609.
I suggest replacing 611 with

$classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( array( '.', '/' ), '-', $template_slug ) );

Change History (2)

#1 @SergeyBiryukov
2 years ago

  • Keywords close added

Hi @isaaclubow, welcome to WordPress Trac! Thanks for the ticket.

We can't just change the way these classes are built due to backwards compatibility concerns, see #23470 / [30100].

In the example above, a page with a /page-templates/full-width.php template would have the following classes: page-template, page-template-page-templates, page-template-full-width, and page-template-page-templatesfull-width-php.

#2 @isaaclubow
2 years ago

Hi @SergeyBiryukov Thanks and I understand! I might just be new to the format but I'm not seeing the comments surrounding https://core.trac.wordpress.org/changeset/30100. The backwards compatibility you mention would only go back to 3.4 (when template subfolders were first supported), right?
I'm getting the feeling there isn't really a good folder/file naming structure that results in all the classes making sense - as page-template-page-templates is semantically silly, too.

Note: See TracTickets for help on using tickets.