modify body_class function to allow non-class items

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.

Change History (18)

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.

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.

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.

@obenland my main issue with leaving it to the theme authors is that it loses the ability to standardize the format. also; the itemprop and itemname (again, using the schema example) could be different for two pieces of content using the same template file, depending on what they may be displaying

considering how many years it took to get theme devs to use the body_class() function, I doubt that anything else will ever get implemented.

Yeah, that also came up during that discussion. :/

I don't doubt that at all. also, please don't misunderstand me, I completely understand (and on it's face, agree with) the reasoning for punting this. just bummed there isn't a way to handle it that doesn't involve breaking years of theme dev habits.