A better body-class function for WordPress

UPDATE– Jan. 25, 2015: moving the parent page name checks into the conditional statement to avoid silent errors // UPDATE– Oct. 20, 2013: Thanks to Marty (see comments below), for convincing us to change out our method for body classes to filter the default WP body_class() function instead of creating our own. It still gives you all the advantages of our original function plus the classes you get by default.

Here’s what it does


Add this stuff to your functions.php file, or just use our html5 shell and it’s built in.


Your Help

Let us know if you see ways to make it better, probably more conditions could be added to the beginning to take into consideration more template types.

Topics covered:
  • Marty Thornley

    You can add or remove any classes you want using the ‘body_class’ filter. I personally use the body classes all the time but if you want simpler, it’s easy to filter it to be whatever you want.

    return apply_filters( ‘body_class’, $classes, $class );


    • Brent Lagerman

      Thanks Marty, you’ve convinced me to retire our function in favor of a filter on the body_class function… My main gripe with it is that it returns SO much crap I don’t need and not some of the obvious stuff a themer would want… Thanks for pointing out this method though, it seems like a better way to do it.

Enable Javascript