WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Plugin API/Filter Reference/body class

Description

The "body_class" filter is used to filter the classes that are assigned to the body HTML element on the current page.

A plugin (or theme) can filter these classes with the code:

<?php add_filter( 'body_class', 'filter_function_name' ) ?>

Where 'filter_function_name' is the function WordPress should call when the classes are being assigned. Note that the filter function must return the array of classes after it is finished processing, or all of the classes will be cleared and could seriously impact the visual state of a user's site.

filter_function_name should be unique function name. It cannot match any other function name already declared.

This filter is used by the get_body_class() function.

Examples

Classes in WordPress Multisite

This could be used to provide custom classes for applying different styles to specific sites in Multisite that all use the same theme:

// Apply filter
add_filter('body_class', 'multisite_body_classes');

function multisite_body_classes($classes) {
        $id = get_current_blog_id();
        $slug = strtolower(str_replace(' ', '-', trim(get_bloginfo('name'))));
        $classes[] = $slug;
        $classes[] = 'site-id-'.$id;
        return $classes;
}

Note that $classes is an array. This means that you could add new classes to it, or unset unwanted classes.

Source File

get_body_class() is located in wp-includes/post-template.php.

See Also

This page is marked as incomplete. You can help Codex by expanding it.