0

I tried with this code in functions.phpof my website's child theme.

add_filter('admin_body_class', 'custom_body_class');
function custom_body_class($classes) {
    if (is_page(8))
        $classes[] = 'home-admin-area';
    return $classes;
}

But the class "home-admin-area" is not added. Is there any error in this code?

Edit 1: I used is_page() function for backend page which was wrong. I tried with this also but it somehow did not work.

add_filter('admin_body_class', 'custom_body_class');
function custom_body_class($classes) {
    if ($_GET['post']==8)
        $classes[] .= ' new-class ';
    return $classes;
}
5
  • Please refer this at once developer.wordpress.org/reference/hooks/admin_body_class and review your code – Pratik Patel Nov 26 '18 at 13:45
  • Already checked this page @PratikPatel and I have tried adding space before and after the class but it didn't help. – Rahul Nov 26 '18 at 13:47
  • 1
    is_page is a front end function, what are you trying to test for on an admin screen? That you’re editing page with id of 8? – Milo Nov 26 '18 at 13:49
  • It's not just a space that you need to add - it has to be handled as a string, not an array (see answer below). – butlerblog Nov 26 '18 at 13:53
  • You're right and I am sorry @Milo I didn't pay attention to this. I wanted to add a class to my home page admin area only. – Rahul Nov 26 '18 at 13:57
2

Use admin_body_class both with global post_id and get_current_screen function:

add_filter('admin_body_class', 'wpse_320244_admin_body_class');

function wpse_320244_admin_body_class($classes) {
    global $post;

    // get_current_screen() returns object with current admin screen
    // @link https://codex.wordpress.org/Function_Reference/get_current_screen
    $current_screen = get_current_screen();

    if($current_screen->base === "post" && absint($post->ID) === 8) {
        $classes .= ' home-admin-area';
    }

    return $classes;
}

You can also use $pagenow variable. It seems that this way would be preferable, because get_current_screen() maybe undefined in some cases:

add_filter('admin_body_class', 'wpse_320244_admin_body_class');

function wpse_320244_admin_body_class($classes) {
    global $post, $pagenow;

    // $pagenow contains current admin-side php-file
    // absint converts type to int, so we can use strict comparison
    if($pagenow === 'post.php' && absint($post->ID) === 8) {
        $classes .= ' home-admin-area';
    }

    return $classes;
}
2
  • It worked and helped me achieve what I wanted but could you please explain it a little? – Rahul Nov 27 '18 at 5:44
  • 1
    I added some explanation – Anton Lukin Nov 27 '18 at 11:08
1

admin_body_class passes its values as a string, not an array. (This differs from body_class which does pass an array. (See the documentation for admin_body_class)

So what you need is:

add_filter('admin_body_class', 'custom_body_class');
function custom_body_class($classes) {
    if (is_page(8))
        $classes .= ' home-admin-area';
    return $classes;
}

Note how it's adding as a string with a leading space.

However, not sure if this will work because I am wondering if you're using the correct filter for what you want to do. Your use of is_page() makes me wonder - is this something you're doing in the admin? Or is this a front end thing?

3
  • Yes I made a mistake. I wanted to add a class to the admin area of my home page for which I cannot use is_page as pointed out by milo in comments. – Rahul Nov 26 '18 at 13:58
  • could you please help me out with this? – Rahul Nov 26 '18 at 18:14
  • I don't know if I can. I'm still not clear on what you're actually trying to do with it. So my suggestion would be trying it without the "if" check first. That way you'll know if you're even close. If that doesn't work at all, then admin_body_class is not the right filter for what you're trying to do. – butlerblog Nov 27 '18 at 3:07

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.