Opened 9 months ago
Last modified 3 months ago
#21766 new feature request
modify body_class function to allow non-class items
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Template | Version: | |
| Severity: | normal | Keywords: | has-patch 2nd-opinion |
| Cc: |
Description
Currently, the body_class only allows for filtering with additional classes. For users that want to add other items, they have to manually edit the theme files. The best example of this is schema.org body elements (itemtype and itemprop).
The diff file shows two things:
- the new body_items function that grabs any items added via filter, if they exist.
- the modified body_class function, which appends the output with the items if they exist.
The patch, as I've written it, allows for this additional filter to be included without interfering with how themes / plugins interact with the current body_class filter, and doesn't require theme authors to include an additional tag in their themes.
Below is an example function that could be placed in a theme or plugin to add other items to the body tag element.
function schema_bodyitems($item) {
if (is_page() ) :
$item[] = 'item-foo="item-bar"';
endif;
$item[] = 'itemtype="http://schema.org/Blog"';
$item[] = 'itemscope=""';
return $item;
}
add_filter('body_items','schema_bodyitems');
Attachments (1)
Change History (9)
I see value in having a way to add new attributes to the <body> tag, this approach still seems kind of scoped to the example use case.
I'm not a fan of having to piggy back on body_class(), but I see why -- body_class is widely deployed and integrated into many themes. However, I still think there is a cleaner approach to this problem that allows body_class to still work while also introducing a new filter to add attributes.
Replying to xenlab:
I see value in having a way to add new attributes to the <body> tag, this approach still seems kind of scoped to the example use case.
I'm not a fan of having to piggy back on body_class(), but I see why -- body_class is widely deployed and integrated into many themes. However, I still think there is a cleaner approach to this problem that allows body_class to still work while also introducing a new filter to add attributes.
based on the method outlined, you could add stand-alone elements as well, not just the item-foo=item-bar setup.
- Keywords dev-feedback removed
Similar discussion for admin body classes: #19460
Replying to scribu:
Similar discussion for admin body classes: #19460
similar, but JS won't correctly solve the issue on the front-end. adding schema items (which is one example, but the best I can think of currently) via JS won't apply until after the page loads, which won't put it in the source code that search engines will crawl. if it isn't in the actual source, it may as well not exist in the eyes of a search engine.
comment:5
markjaquith — 8 months ago
I will deny having written this:
<?php
class WP_Body_Class_Injection_Plugin {
public function __construct(){
add_filter( 'body_class', array( $this, 'body_class' ) );
}
public function body_class( $classes ) {
$backtrace = debug_backtrace();
if ( $backtrace[4]['function'] === 'body_class' )
echo " foo='bar' ";
return $classes;
}
}
new WP_Body_Class_Injection_Plugin;
comment:6
SergeyBiryukov — 8 months ago
- Component changed from General to Template
- Version trunk deleted

removing commented out items