13

I don't need this whole mess of classes like this...

<body class="page page-id-829 page-template page-template-page-template-portfolio-php portfolio">

I'd like something like this...

<body class="portfolio">

Is there a filter snippet somewhere that has a list of all the classes and I can then just uncomment the stuff I don't want to see in the body class.

Thanks.

  • The real question is why would you want to do that? Having those extra classes doesn't matter in the grand scheme of things, and it's nice to have them for styling, if you want to reference them later. – Otto Apr 28 '11 at 21:08
  • 3
    @Otto - i can fully understand her/him. If you're going to play around with adding classes in eg. firebug, then it's really hard to handle this. FF is often buggy and the classname you're writing out of the viewport. I turn off lots of post- and body-classes during dev myself - it makes life easier. – kaiser Apr 28 '11 at 23:52
  • Ahh, yeah, I don't use Firebug. Its interface is deplorable. – Otto Apr 29 '11 at 18:59
27

You can configure the $whitelist array in this function to filter out all other unwanted classes.

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

function wpse15850_body_class( $wp_classes, $extra_classes ) {

    // List of the only WP generated classes allowed
    $whitelist = array( 'portfolio', 'home', 'error404' );

    // Filter the body classes
    $wp_classes = array_intersect( $wp_classes, $whitelist );

    // Add the extra classes back untouched
    return array_merge( $wp_classes, (array) $extra_classes );
}
13

Just an addition to @Geert answer (added a blacklist too) :)

Please be so nice to mark @Geert s answer as solution (not this one).

function wpse15850_body_class( $wp_classes, $extra_classes )
{
    // List of the only WP generated classes allowed
    $whitelist = array( 'home', 'blog', 'archive', 'single', 'category', 'tag', 'error404', 'logged-in', 'admin-bar' );

    // List of the only WP generated classes that are not allowed
    $blacklist = array( 'home', 'blog', 'archive', 'single', 'category', 'tag', 'error404', 'logged-in', 'admin-bar' );

    // Filter the body classes
    // Whitelist result: (comment if you want to blacklist classes)
    $wp_classes = array_intersect( $wp_classes, $whitelist );
    // Blacklist result: (uncomment if you want to blacklist classes)
    # $wp_classes = array_diff( $wp_classes, $blacklist );

    // Add the extra classes back untouched
    return array_merge( $wp_classes, (array) $extra_classes );
}
add_filter( 'body_class', 'wpse15850_body_class', 10, 2 );
  • Although a very good working solution, I still find 'no-js' is added as a class – henrywright Jan 27 '14 at 12:07
  • 1
    Regarding the no-js class - I think it might be a BuddyPress thing. I'll investigate! – henrywright Jan 27 '14 at 12:36
  • 1
    no-js or likewise js are usually dealt with using javascript/jquery, so I'm guessing it was a script that you'd need to dequeue – Bryan Willis Dec 19 '15 at 3:35
  • no-js will be added by the server, but removed with a script if javascript is allowed to run on the browser. – Davey Apr 30 at 8:10
4

I would recommend merely omitting the <?php body_class(); ?> template tag, if you have no need for its output.

Just apply class="portfolio" hard-coded into the <body> tag.

  • if you want to host your theme at the wp.org repo, then you don't have this choise (just a sidenote). – kaiser Apr 28 '11 at 23:46
  • 1
    LOL, I know... I've reviewed one or two Themes for the WPORG repository. :) I assumed from the question, though, that this Theme is for personal use, rather than general, public distribution. – Chip Bennett Apr 29 '11 at 0:51
2

Just place the classes of your css that you want to remove in $ class_delete

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

function wpse15850_body_class( $wp_classes, $extra_classes ) {

    # List tag to delete
    $class_delete = array('tag');

    # Verify if exist the class of WP in $class_delete
    foreach ($wp_classes as $class_css_key => $class_css) {
        if (in_array($class_css, $class_delete)) {
            unset($wp_classes[$class_css_key]);
        }
    }

    // Add the extra classes back untouched
    return array_merge( $wp_classes, (array) $extra_classes );
}
1

This will give every page a body class of only 'portfolio'. The first argument is the array of generated body classes that would normally appear. The second argument is an array of classes passed into the body class function (e.g. body_class('portfolio'); would make the second argument in this function array( 'portfolio' ) ).

function wpse15850_body_classes( $classes, $class ){
    return array( 'portfolio' );
}

add_filter( 'body_class', 'wpse15850_body_classes', 10, 2 );
1
Try the following...

function var_template_include( $t ){
    $basename = basename($t);
    $templatename = substr($basename, 0,strrpos($basename,'.')); 
    $GLOBALS['current_theme_template'] = $templatename;

    return $t;
}
add_filter( 'template_include', 'var_template_include', 1000 );


function current_template( $echo = false ) {
    if( !isset( $GLOBALS['current_theme_template'] ) ) {
        return false;
    } if( $echo ) {
        echo $GLOBALS['current_theme_template'];
    } else {
        return  $GLOBALS['current_theme_template'];
    }   
}

function body_template_as_class() {
echo 'class="'.current_template().'"';
}
0

If you only want to remove a specific class from body Tag you should to this:

add_filter('body_class', 'remove_body_class', 20, 2);

function remove_body_class($wp_classes) {
    foreach($wp_classes as $key => $value)
        {
            if ($value == 'portfolio') unset($wp_classes[$key]); //  Replaces "portfolio" and removes it
        }

    return $wp_classes;
}
0
function alpha_remove_class($wp_classes){
  unset( $wp_classes[ array_search( "first_class", $wp_classes ) ] );

  return $wp_classes;
}
add_filter( 'body_class', 'alpha_remove_class' );
  • Welcome to WordPress Stack Exchange! Could you please edit your answer, using the edit link above and explain what this code does, where you would put this code, and how it's different or better than any of the previously posted answers? Code only answers are usually discouraged without any explanation. – Howdy_McGee Sep 5 '18 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.