Home / body_class / Setup whitelist body_class to remove unwanted classes
  • How to Setup whitelist body_class to remove unwanted classes

    setup-whitelist-body_class-to-remove-unwanted-classes

    Adding this snippet to the functions.php of your wordpress theme will let you create a white-list of terms to include in body_class. This is a great way to filter out many of the unwanted classes added

    wordpress snippet : PHP

    <>
    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);
    }
    

    ( WordPress codex functions, hooks, in this snippet. )

    home, body_class, add_filter, wp,