Make WordPress Core

Opened 13 years ago

Last modified 3 days ago

#19736 reopened enhancement

Add default body_class classes for Theme Names

Reported by: cais's profile cais Owned by: audrasjb's profile 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)

body-class-defaults.patch (1.7 KB) - added by cais 13 years ago.
revised-body-class-defaults.patch (805 bytes) - added by cais 13 years ago.
Uses get_option versus get_theme_data
after-body-class.png (689.4 KB) - added by rinkalpagdar 9 days ago.

Download all attachments as: .zip

Change History (31)

#1 @GaryJ
13 years ago

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.

#2 follow-up: @cais
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: @nacin
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: @GaryJ
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 @cais
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: @cais
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: @SergeyBiryukov
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 @cais
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.

@cais
13 years ago

Uses get_option versus get_theme_data

#9 @c3mdigital
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.

#10 @SergeyBiryukov
11 years ago

  • Keywords needs-testing removed
  • Milestone Awaiting Review deleted

#11 @johnjamesjacoby
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

https://github.com/user-attachments/assets/dffa6d5a-da71-4c3a-9391-72144d556271

  • For a child theme named childtheme having twentytwentyfour as parent theme

https://github.com/user-attachments/assets/32b89d6d-fd6e-4ccf-b0c5-0a2e589aacd6

## Trac ticket: https://core.trac.wordpress.org/ticket/19736

#13 @nirajgirixd
4 months ago

It's a small PR. Could someone please review and either approve or provide feedback?

cc: @nacin, @SergeyBiryukov, @johnjamesjacoby

#14 @poena
2 months ago

  • Keywords needs-testing added
  • Milestone changed from Future Release to 6.8
  • Severity changed from minor to normal

@poena commented on PR #7038:


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)

@poena commented on PR #7038:


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 @rinkalpagdar
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

  1. ✅ Issue resolved with patch.

#18 @audrasjb
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 @rinkalpagdar
9 days ago

@audrasjb I am using Twenty Twenty-Five 1.0 theme as mentioned in the environment details in the test report with no child theme. I have attached a screenshot of the <body> class I am getting after applying a patch.
https://core.trac.wordpress.org/attachment/ticket/19736/after-body-class.png

Last edited 9 days ago by rinkalpagdar (previous) (diff)

This ticket was mentioned in Slack in #core by audrasjb. View the logs.


7 days ago

#21 @audrasjb
7 days ago

  • Owner set to audrasjb
  • Status changed from reopened to accepted

Self assigning for final review and commit.

#22 @audrasjb
7 days ago

  • Keywords needs-dev-note added

#23 @audrasjb
6 days ago

  • Keywords commit added; 2nd-opinion needs-testing removed

Alright, let's ship it in 6.8.

#24 @audrasjb
6 days ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 59698:

Themes: Add wp-theme-<name> and wp-child-theme-<name> classes to body_class.

This changeset introduces new classes to the body tag. The classes wp-theme-<name> and wp-child-theme-<name> (when the current theme is a child theme) are added, where <name> represents the sanitized name of the active theme.

Props cais, GaryJ, nacin, SergeyBiryukov, johnjamesjacoby, nirajgirixd, poena, audrasjb, rinkalpagdar.
Fixes #19736.

#25 @audrasjb
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.

#26 @audrasjb
6 days ago

  • Keywords needs-unit-tests added

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

#28 @mikinc860
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.

Note: See TracTickets for help on using tickets.