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 want to extend both body classes for the front-end as well as for the admin area. I have seen some snippets floating around, which led me to the following

add_filter('body_class','add_custom_body_classes');
add_filter('admin_body_class', 'add_custom_body_classes');

Now it seems to me, that I do have to do a type check like the following, because in the case of the backend extension the variable is a string and not an array.

function add_custom_body_classes($classes) {
    $someClassA = 'someText';
    $someClassB = 'someText';

    if (is_array($classes)) {
        $classes[] = $someClassA;
        $classes[] = $someClassB;
    }
    else {
        $classes .= ' ' . $someClassA;
        $classes .= ' ' . $someClassB;
    }
    return $classes;
}

Is this correct or what is it I am missing?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Your snippet adds the following classes to the body tag on the front end,

someText
someText

Your snippet adds the following classes to the body tag on the back end,

someClassA
someClassB

If that is you intended result, then yes that works and is correct.

You could also do,

function add_custom_body_classes($classes) {

    $someClassA = 'someText';
    $someClassB = 'someText';

    if (is_array($classes)) {
        $classes[] = $someClassA;
        $classes[] = $someClassB;
    }
    elseif (is_admin()) {
        $classes .= ' ' . someClassA;
        $classes .= ' ' . someClassB;
    }
    return $classes;
}

add_filter('body_class','add_custom_body_classes');
add_filter('admin_body_class', 'add_custom_body_classes');

By using is_admin conditional statement to make sure you are on the admin page before adding specific classes.

If none of the above is correct, and you want the same classes (someText) to appear on both front and back end, then you need to modify...

$classes .= ' ' . someClassA;
$classes .= ' ' . someClassB;

To show,

$classes .= ' ' . $someClassA;
$classes .= ' ' . $someClassB;

As you were missing $ in your variable names.

Assuming you want the same classes on front and back ends you can also do an array to string conversion like,

function add_custom_body_classes($classes) {

$classes = array('classA', 'classB', 'classC');
$string  = implode($classes, ' ');

    if (!is_admin() && is_array($classes)) {
        return $classes;
    }
    elseif (is_admin()) {
        return $string;
    }
}

add_filter('body_class','add_custom_body_classes');
add_filter('admin_body_class', 'add_custom_body_classes');
share|improve this answer
    
Thanks for your confimation and your idea to use implode! The missing "$" was by accident and I've corrected it. Lastly: Do you have any idea, why the classes variable isn't of the same type in these two callbacks? –  SunnyRed Sep 17 '12 at 21:48
1  
No problem and yes, poor design choice/consistency. You get that from time to time when dealing with core. Look at: wp-includes/post-template.php & wp-admin/admin-header.php one is concatenating a rather simple set strings (admin-header.php) while the other is iterating over a more complex array which deals with template files in your theme. Concatenating strings doesn't make sense in that case. Admittedly it could do with some standardization. If you want, you could always write a patch and submit it to core core.trac.wordpress.org –  userabuser Sep 17 '12 at 22:42
add comment

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.