Working with the WordPress admin_body_class function




When you modify the body_class function, you hook into the filter like this:

add_filter( 'body_class', 'category_id_class' );
// add category nicenames in body and post class
function category_id_class( $classes ) {
 global $post;
 foreach ( ( get_the_category( $post->ID ) ) as $category ) {
  $classes[] = $category->category_nicename;
 }
 return $classes;
}

If you break that down, you’ll notice that adding a new class name requires extending the $classes array. Straightforward enough, though for some reason when you attempt to work with the admin_body_class function, this approach causes the following error:

Fatal error: [] operator not supported for strings

That’s because it uses a string instead of an array.

As long as your aware of this difference, working with the admin_body_class function is just as straightforward.

add_filter( 'admin_body_class', 'custom_admin_body_class' );
function custom_admin_body_class( $classes ) {
        $classes .= 'my-class'; 
 return $classes;
}

Reference: Github



Related Post


Latest Post


Recent Posts Widget

Make sure to never miss a thing...

Get the latest news from the creative industry along with other creative goodies, conveniently delivered to social media.