Opened 9 months ago

Last modified 3 months ago

#21766 new feature request

modify body_class function to allow non-class items

Reported by: norcross 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:

  1. the new body_items function that grabs any items added via filter, if they exist.
  1. 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)

post-template.php.diff (1.2 KB) - added by norcross 9 months ago.
removing commented out items

Download all attachments as: .zip

Change History (9)

removing commented out items

comment:1 follow-up: ↓ 2   xenlab9 months ago

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.

Last edited 9 months ago by xenlab (previous) (diff)

comment:2 in reply to: ↑ 1   norcross9 months ago

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.

comment:3 follow-up: ↓ 4   scribu9 months ago

  • Keywords dev-feedback removed

Similar discussion for admin body classes: #19460

comment:4 in reply to: ↑ 3   norcross9 months ago

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.

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;
  • Component changed from General to Template
  • Version trunk deleted

#23537 seems better than hacking the body_class.

@ryanve I agree. the hack 'works' but it doesn't address the underlying concern. I'm not sure what else would be relevant to add (body ID, maybe?) but having more flexibility would be great.

Note: See TracTickets for help on using tickets.