1

I have the following function in a plugin I have built:

static function filter_add_body_classes( $body_classes ) {

    $new_classes = array();

    // Only when logged in
    if ( ! is_user_logged_in() ) {
        return $body_classes;
    }

    // If viewing the site
    if ( ! is_admin() ) {
        $new_classes[] = 'sat-site';
    }

    // If theming is enabled
    if ( Skizzar_Admin_Theme::is_themed() ) {
        $new_classes[] = 'sat-theme';
    }

    // If a custom logo image is provided and the menu logo hiding option is not enabled
    if ( Skizzar_Admin_Theme_Options::get_saved_network_option( 'logo-image' ) && ! Skizzar_Admin_Theme_Options::get_saved_network_option( 'hide-menu-logo' ) ) {
        $new_classes[] = 'sat-custom-logo';
    }

    // If hide-top-paging option is enabled
    if ( Skizzar_Admin_Theme_Options::get_saved_network_option( 'hide-top-paging' ) ) {
        $new_classes[] = 'sat-hide-top-paging';
    }

    // If hide-post-search option is enabled
    if ( Skizzar_Admin_Theme_Options::get_saved_network_option( 'hide-post-search' ) ) {
        $new_classes[] = 'sat-hide-post-search';
    }

    // If hide-top-bulk option is enabled
    if ( Skizzar_Admin_Theme_Options::get_saved_network_option( 'hide-top-bulk' ) ) {
        $new_classes[] = 'sat-hide-top-bulk';
    }

    // If hide-user-role-changer
    if ( Skizzar_Admin_Theme_Options::get_saved_network_option( 'hide-user-role-changer' ) ) {
        $new_classes[] = 'sat-hide-user-role-changer';
    }

    // If hide-view-switch option is enabled
    if ( Skizzar_Admin_Theme_Options::get_saved_network_option( 'hide-view-switch' ) ) {
        $new_classes[] = 'sat-hide-view-switch';
    }

    // If hide-media-bulk-select option is enabled
    if ( Skizzar_Admin_Theme_Options::get_saved_network_option( 'hide-media-bulk-select' ) ) {
        $new_classes[] = 'sat-hide-media-bulk-select';
    }

    // If hide-comment-type-filter option is enabled
    if ( Skizzar_Admin_Theme_Options::get_saved_network_option( 'hide-comment-type-filter' ) ) {
        $new_classes[] = 'sat-hide-comment-type-filter';
    }

    // If enable-notification-center option is enabled
    if ( Skizzar_Admin_Theme_Options::get_saved_network_option( 'enable-notification-center' ) ) {
        $new_classes[] = 'sat-notification-center';
    }

    // Merge & return
    if ( is_array( $body_classes ) ) {
        return array_merge( $body_classes, $new_classes );
    }
    return $body_classes . ' ' . implode( ' ', $new_classes ) . ' ';

}

In my main plugin file I call it in 3 places using the following filters:

add_filter( 'body_class', array( 'Skizzar_Admin_Theme_Setup', 'filter_add_body_classes' ) );
add_filter( 'admin_body_class', array( 'Skizzar_Admin_Theme_Setup', 'filter_add_body_classes' ));
add_filter( 'login_body_class', array( 'Skizzar_Admin_Theme_Setup', 'filter_add_body_classes' ) );

This should add classes to the body based on options users pick for their plugin. The classes should be added to the admin body, site and login page. However, currently, it just adds to the site - so essentially admin_body_class and login_body_class are doing nothing.

Can anyone help me trouble shoot this - not getting any errors, just it's not doing anything

  • Are you hooking the filter inside any function? Maybe it's being called too late on admin. Or can you try debug to see if in admin it's returning on the first if where it check the user? – Bruno Cantuaria Mar 24 '16 at 14:47
  • Hi @BrunoCantuaria - my filters aren't wrapped in a function no - they are called on the main plugin file page. How would I go about debugging this? – Sam Skirrow Mar 24 '16 at 15:00
2

About your issue in admin body class, WP Codex documentation admin_body_class, class as string ( not array ), there are some of your function like get_saved_network_option not returning true that cause $new_classes not setted in array.

This code correct, we need return in string, but $new_classes maybe in string empty or not array.

return $body_classes . ' ' . implode( ' ', $new_classes ) . ' ';

For login page class, you are correct to set login_body_class in array, BUT you set condition too early as @Bruno Cantuaria comment, take a look your code:

// Only when logged in
if ( ! is_user_logged_in() ) {
    return $body_classes;
}

So thats why you class settings not return, it only work if user not logged-in. I hope this helps.

  • Hi @jevuska. Thanks for your reply - I'm afraid I don't completely understand - what should I change in my code in order to fix my issue? – Sam Skirrow Mar 24 '16 at 20:10
  • For on login page, I think you just need to remove the code that I mention if ( ! is_user_logged_in() ) { return $body_classes; }, if not, you need to decide which class that used in login page and put inside that code. – Jevuska Mar 24 '16 at 20:19
  • I see, you are right - this does work for the login page - my main issue though is gettig these classes to add to the admin body, and this does not work for that – Sam Skirrow Mar 24 '16 at 20:46
  • Sorry I update my statement for admin body class, you need to test all class and enable all option that mention on code, and see if one or more class added in body. – Jevuska Mar 24 '16 at 21:11

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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