Using WordPress ‘admin_body_class’ PHP filter

The admin_body_class WordPress PHP filter allows you to modify the CSS classes for the body tag in the WordPress admin area.

Usage

add_filter('admin_body_class', 'your_custom_function');
function your_custom_function($classes) {
    // your custom code here
    return $classes;
}

Parameters

  • $classes (string): A space-separated list of CSS classes.

More information

See WordPress Developer Resources: admin_body_class Please note that not all core admin classes are filterable. Specifically, wp-admin, wp-core-ui, and no-js cannot be removed.

Examples

Adding a custom CSS class to the admin body

This example adds a custom CSS class called my-custom-class to the body tag in the admin area.

add_filter('admin_body_class', 'add_my_custom_class');
function add_my_custom_class($classes) {
    $classes .= ' my-custom-class';
    return $classes;
}

Adding a CSS class based on user role

This example adds a CSS class based on the user’s role, such as role-administrator or role-editor.

add_filter('admin_body_class', 'add_role_based_class');
function add_role_based_class($classes) {
    $user = wp_get_current_user();
    if (!empty($user->roles[0])) {
        $classes .= ' role-' . $user->roles[0];
    }
    return $classes;
}

Adding a CSS class based on the current post type

This example adds a CSS class based on the current post type being edited, such as post-type-page or post-type-post.

add_filter('admin_body_class', 'add_post_type_class');
function add_post_type_class($classes) {
    global $post;
    if (isset($post) && isset($post->post_type)) {
        $classes .= ' post-type-' . $post->post_type;
    }
    return $classes;
}

Adding a CSS class based on the current screen

This example adds a CSS class based on the current admin screen, such as screen-dashboard or screen-edit-post.

add_filter('admin_body_class', 'add_screen_based_class');
function add_screen_based_class($classes) {
    $screen = get_current_screen();
    if (isset($screen) && isset($screen->id)) {
        $classes .= ' screen-' . $screen->id;
    }
    return $classes;
}

Removing a specific CSS class from the admin body

This example removes a specific CSS class, in this case auto-fold, from the body tag in the admin area.

add_filter('admin_body_class', 'remove_auto_fold_class');
function remove_auto_fold_class($classes) {
    $classes = str_replace('auto-fold', '', $classes);
    return $classes;
}