posts - How do I add a custom body class to the admin area of a page?

admin2025-06-06  2

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;
}

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;
}
Share Improve this question edited Nov 26, 2018 at 23:10 butlerblog 5,1413 gold badges28 silver badges44 bronze badges asked Nov 26, 2018 at 13:42 RahulRahul 2291 gold badge4 silver badges12 bronze badges 5
  • Please refer this at once developer.wordpress/reference/hooks/admin_body_class and review your code – Pratik Patel Commented Nov 26, 2018 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 Commented Nov 26, 2018 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 Commented Nov 26, 2018 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 Commented Nov 26, 2018 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 Commented Nov 26, 2018 at 13:57
Add a comment  | 

2 Answers 2

Reset to default 3

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/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;
}

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?

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749148910a316782.html

最新回复(0)