Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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
2  
@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

5 Answers 5

up vote 8 down vote accepted

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

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

function my_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);
}
share|improve this answer
    
smart solution! +1 –  kaiser Apr 28 '11 at 23:49
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().'"';
}
share|improve this answer

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 );
share|improve this answer
    
Although a very good working solution, I still find 'no-js' is added as a class –  henrywright Jan 27 at 12:07
1  
Regarding the no-js class - I think it might be a BuddyPress thing. I'll investigate! –  henrywright Jan 27 at 12:36

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.

share|improve this answer
    
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
1  
The magic word 'portfolio' ;) –  kaiser Apr 29 '11 at 1:24

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 );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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