Simple Add a Class to the Body Tag in WordPress

July 11th, 2012 by

This is something that is extremely easy once you realize that the hook body_class exists. It is important to pass an variable argument (it can be anything, just has to be there), the hook will pass the current array of classes to that argument. Simple usage of this hook is as follows:

function add_body_class ($classes) {
    $classes[] = 'myclass';
    return $classes;

add_filter('body_class', 'add_body_class');

What this does is add the class “myclass” to the <body> class.

Here’s an example of adding a specific class to the homepage, add this to your functions.php (for a theme) or your plugin functions page (for a plugin):

function add_home_class ($classes) {
   if (is_home()) $classes[] = 'myclass';
    return $classes;

add_filter('body_class', 'add_home_class');

This can be adapted in any way shape or form, just change the logic in the if statement that wraps around the filter.

Have fun!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>