最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - body_class hook for admin pages

programmeradmin0浏览0评论

The body_class hook seem only to work with non-admin pages. When I do

add_filter('body_class', 'add_body_classes');
function add_body_classes($classes) {
        $classes[] = 'myclass';
        return $classes;
}

Is there another hook i should be using if i want to add a class to the body of an admin page?

The body_class hook seem only to work with non-admin pages. When I do

add_filter('body_class', 'add_body_classes');
function add_body_classes($classes) {
        $classes[] = 'myclass';
        return $classes;
}

Is there another hook i should be using if i want to add a class to the body of an admin page?

Share Improve this question asked Mar 5, 2012 at 6:51 ltfishieltfishie 7652 gold badges9 silver badges28 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 26

Admin pages don't use the body_class filter, use the admin_body_class filter to add classes to admin body tag instead.

Note that $classes in that case is a string, not an array.

Mamaduka answer pointed me to the right direction, here is the code for adding classes to the body in the dashboard.

The callback function should return a valid value for the HTML class attribute, that is space separated class names, also, don't forget to prepend (or append) any existing classes, you should understand by reading the code.

add_filter( 'admin_body_class', 'my_admin_body_class' );

/**
 * Adds one or more classes to the body tag in the dashboard.
 *
 * @link https://wordpress.stackexchange/a/154951/17187
 * @param  String $classes Current body classes.
 * @return String          Altered body classes.
 */
function my_admin_body_class( $classes ) {
    return "$classes my_class";
    // Or: return "$classes my_class_1 my_class_2 my_class_3";
}

Adding multiple classes, and especially when they must display based on some conditions, may result in an inconsistent HTML output. To format it correctly and also have the ability to remove duplicates or existing classes, we can convert them to array and in the end join back to string:

add_filter('admin_body_class', static function ($classes) {
    $classes = explode(' ', $classes);
    
    $classes = array_merge($classes, [
        'my-class-one',
        'my-class-two',
        'my-class-three',
        some_function(),
        another_function(),
    ]);

    return implode(' ', array_unique($classes));
});

one of the hooks i use for editing WP admin/Dashboard admin_head

    function remove_screen_options(){ 

    $hideCSS = '<style>#screen-meta-links { display: none; }</style>';

} 

Will put the css in your admin part

And by using class .wp-admin you could style the body part of admin.

Good luck

发布评论

评论列表(0)

  1. 暂无评论