Opened 13 years ago
Last modified 3 days ago
#19736 reopened enhancement
Add default body_class classes for Theme Names
Reported by: | cais | Owned by: | audrasjb |
---|---|---|---|
Milestone: | 6.8 | Priority: | normal |
Severity: | normal | Version: | 3.3.1 |
Component: | Themes | Keywords: | needs-dev-note has-patch has-unit-tests |
Focuses: | css | Cc: |
Description
Although this may be more pure enhancement and quite possibly Mallory-Everest in nature, the idea of having a default body_class
output for the Theme name seems to be a reasonable one.
IF this is implemented it should also include a default class for a Child-Theme name as well as the Parent-Theme name.
To that end, I am suggesting the following patch to the body_class
function.
Attachments (3)
Change History (31)
#2
follow-up:
↓ 4
@
13 years ago
The Theme Name (aka theme slug) is already exposed in the path to the Theme's stylesheet ... this doesn't do anything more than add an additional reference for all intent and purpose.
One use for this is allowing plugin specific styles depending on what Theme is active; Multi Site installations come to mind as potential beneficiaries.
#3
follow-up:
↓ 6
@
13 years ago
The patch calls the expensive get_theme_data() function that reads directly from the file.
In order to implement this, we should use the existing theme slug. Example:
'theme-' . sanitize_html_class( get_option( 'template' ) )
While it's a cool idea, I don't think it's going to be necessary for most use cases.
#4
in reply to:
↑ 2
;
follow-up:
↓ 5
@
13 years ago
Replying to cais:
The Theme Name (aka theme slug) is already exposed in the path to the Theme's stylesheet
Assuming that the theme doesn't dequeue the default style sheet reference in favour of one that's created in the wp-contents directory (very few would do that, admittedly, but it's still possible to do (as per a theme I'm building at the moment). As such, forcing a theme name to be exposed by a body class isn't being considerate.
If a theme *does* want to play nicely with certain plugins, then it can add it's own class in 5 lines of code, or the plugins can handle providing theme-specific styles if they are providing any styles at all, including detecting the active theme.
#5
in reply to:
↑ 4
@
13 years ago
Replying to GaryJ:
Replying to cais:
The Theme Name (aka theme slug) is already exposed in the path to the Theme's stylesheet
If a theme *does* want to play nicely with certain plugins, then it can add it's own class in 5 lines of code, or the plugins can handle providing theme-specific styles if they are providing any styles at all, including detecting the active theme.
I look at this as more for a third-party to be able to have themes and plugins able to play nicely with each other rather than pushing the theme / plugin authors to differentiate between which ones they will favor and which they will not; but that also takes us into what is becoming a non-related matter.
Again, as I prefaced above, this may be more a Mallory-Everest idea with a working patch that proves it out. If a developer, such as you have chosen to do, wants to "hide" the theme name they can just as easily remove these or any other classes from the body_class
output.
#6
in reply to:
↑ 3
;
follow-up:
↓ 7
@
13 years ago
Replying to nacin:
The patch calls the expensive get_theme_data() function that reads directly from the file.
In order to implement this, we should use the existing theme slug. Example:
'theme-' . sanitize_html_class( get_option( 'template' ) )
While it's a cool idea, I don't think it's going to be necessary for most use cases.
get_theme_data
may be expensive but I could not immediately think of another method to resolve the 'child-theme' name. Therefore I used it for both, but using the get_option( 'template' )
approach is definitely better for the Parent-Theme.
#7
in reply to:
↑ 6
;
follow-up:
↓ 8
@
13 years ago
Replying to cais:
get_theme_data
may be expensive but I could not immediately think of another method to resolve the 'child-theme' name.
'child-theme-' . sanitize_html_class( get_option( 'stylesheet' ) )
#8
in reply to:
↑ 7
@
13 years ago
Replying to SergeyBiryukov:
Replying to cais:
get_theme_data
may be expensive but I could not immediately think of another method to resolve the 'child-theme' name.
'child-theme-' . sanitize_html_class( get_option( 'stylesheet' ) )
Doh! I should have known it was that straight-forward.
Note to self: find/create a list of all available values found via get_option
.
#9
@
11 years ago
- Resolution set to wontfix
- Status changed from new to closed
No activity in 20 months and an existing body_class filter. Closing as wont fix. If anyone want to further discuss this please reopen.
#11
@
4 years ago
- Focuses css added
- Keywords 2nd-opinion needs-refresh added
- Milestone set to Future Release
- Resolution wontfix deleted
- Status changed from closed to reopened
With the advent of block themes in WordPress 5.8 and above, I'm reopening this for discussion and future consideration.
My use case is providing CSS in block plugins to target specific themes. This may be to override something weird in the theme, or to make some HTML output blend in better with existing theme styling.
This ticket was mentioned in PR #7038 on WordPress/wordpress-develop by @nirajgirixd.
7 months ago
#12
- Keywords needs-refresh removed
## Description
This PR introduces new classes to the body tag in WordPress themes. The classes wp-theme-<name>
and wp-child-theme-<name>
(if child theme) are added, where <name>
represents the sanitized name of the active theme and child theme, respectively.
## Changes Made
- Added
wp-theme-<name>
class to the body tag for the current theme. - Added
wp-child-theme-<name>
class to the body tag for the current theme (if child theme).
## Screenshots
- For
twentytwentyfour
theme
- For a child theme named
childtheme
havingtwentytwentyfour
as parent theme
## Trac ticket: https://core.trac.wordpress.org/ticket/19736
#13
@
4 months ago
It's a small PR. Could someone please review and either approve or provide feedback?
cc: @nacin, @SergeyBiryukov, @johnjamesjacoby
#14
@
2 months ago
- Keywords needs-testing added
- Milestone changed from Future Release to 6.8
- Severity changed from minor to normal
2 months ago
#15
If these class names are intended to help plugins with theme specific styles, they also need to be present in the Site Editor.
(They are already available in the Customizer preview)
4 weeks ago
#16
I checked again, neither of the existing body classes are added in the Site Editor. I am not sure why.
It should be treated as a separate issue and not block this update.
#17
@
2 weeks ago
Test Report
Description
This report validates whether the indicated patch works as expected.
PR tested: https://github.com/WordPress/wordpress-develop/pull/7038
Environment
- WordPress: 6.8-alpha-59274-src
- PHP: 8.2.25
- Server: nginx/1.27.2
- Database: mysqli (Server: 8.0.40 / Client: mysqlnd 8.2.25)
- 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
- ✅ Issue resolved with patch.
#18
@
9 days ago
@rinkalpagdar could you please share some details about your tests?
- Which theme did you install? Was it a child theme?
- Which class name did you get inside the <body> tag? Does it contain the theme slug?
Thanks
#19
@
9 days ago
This ticket was mentioned in Slack in #core by audrasjb. View the logs.
7 days ago
#21
@
7 days ago
- Owner set to audrasjb
- Status changed from reopened to accepted
Self assigning for final review and commit.
#23
@
6 days ago
- Keywords commit added; 2nd-opinion needs-testing removed
Alright, let's ship it in 6.8.
#25
@
6 days ago
- Keywords needs-patch added; has-patch commit removed
- Resolution fixed deleted
- Status changed from closed to reopened
Hmm… would be nice to add some unit tests.
Reopening for follow-up unit test work.
This ticket was mentioned in PR #8193 on WordPress/wordpress-develop by @sukhendu2002.
5 days ago
#27
- Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed
Trac ticket: https://core.trac.wordpress.org/ticket/19736
#28
@
3 days ago
Test Report
This report validates whether the indicated patch works as expected.
Tested Patch: https://github.com/WordPress/wordpress-develop/pull/8193
Environment
OS: Windows 11
Web Server: WordPress Playground
PHP: Playground Default
WordPress: 6.8-alpha-20250125.203533
Browser: Chrome
Theme: Twenty Twenty-Five v1.0
Active Plugins: None
Actual Results
✅ Issue resolved with a patch.
What about themes that didn't want the theme name to be exposed in the source?
It's so trivial for themes to add in a body_class value themselves if they need it, that I can't see the value of forcing all themes to show it.