#11722 closed defect (bug) (fixed)
body_class() creates an invalid class when page template contains a . [full stop]
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | 3.0 | Priority: | low |
Severity: | minor | Version: | 3.0 |
Component: | Template | Keywords: | has-patch |
Focuses: | Cc: |
Description
Confirmed on Trunk and 2.9.1
Steps to reproduce
- Create a new file called template.contact.php that contains the template code:
/* Template Name: Contact */
Create a page through the WordPress admin and select the template.
View the newly created page, view source and notice that body has a class of "page-template-template.contact-php".
Weep in the knowledge that you cannot use css to target that class.
Expected result
Full stops should be converted to a hyphen.
Attachments (2)
Change History (8)
#1
@
15 years ago
- Summary changed from body_class() creates an invalid class when page template contains a . [dot] to body_class() creates an invalid class when page template contains a . [full stop]
The full stop in the file extension is properly escaped.
This happens in line 440 of wp-includes/post-template.php
$classes[] = 'page-template-' . str_replace( '.php', '-php', get_post_meta( $pageID, '_wp_page_template', true ) );
Attached patch simple removes the str_replace and uses the sanitize_html_class function instead.
There could be a somewhat minor backwards compat issue as sanitize_html_class simple removes invalid characters instead replacing them with a hyphen which means that the file extension is sanitized to "php" instead of "-php".
Also I pass an empty fallback to sanitize_html_class, is there something better we could show in the unlikely event that someone has a filename that contains only invalid characters. Perhaps just the string "invalid-filename" or something.
#4
@
15 years ago
post-template.2.diff
This patch would be safer for backwards compatibility and is also prettier for my specific case as the template.contact.php becomes template-contact-php instead of templatecontactphp.
It would be nice if sanitize_html_class replaced some characters with hyphens and removed others, perhaps by pushing the string through sanitize_title first?
sanitize_html_class for page templates