X

Setup Whitelist body_class to Remove Unwanted Classes

Do you want to create a whitelist of terms to include in body_class? A whitelist is a great way to filter out many unwanted classes added in your theme. Simply add the following code to the functions.php file of your WordPress theme to set up whitelist body_class.

add_filter('body_class', 'wps_body_class', 10, 2);

function wps_body_class($wp_classes, $extra_classes)
{
    // List of classes allowed
    $whitelist = array('portfolio', 'home', 'error404');


    $wp_classes = array_intersect($wp_classes, $whitelist);
    return array_merge($wp_classes, (array) $extra_classes);
}

We hope this code helped you setup whitelist body_class to remove unwanted classes. You may also want to check out our guide on add custom body_class using wp_is_mobile.

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!