Make WordPress Core

Opened 13 years ago

Last modified 5 days ago

#19736 reopened enhancement

Add default body_class classes for Theme Names

Reported by: cais's profile cais Owned by:
Milestone: 6.8 Priority: normal
Severity: normal Version: 3.3.1
Component: Themes Keywords: has-patch 2nd-opinion needs-testing
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 (2)

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

Download all attachments as: .zip

Change History (19)

#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.


6 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
7 weeks ago

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

@poena commented on PR #7038:


7 weeks 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:


3 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
5 days 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.
Note: See TracTickets for help on using tickets.