custom css for admin only

I need custom CSS for admin. So if user is not logged in or is anything else but admin there is one CSS, and for admin only different CSS.

I tried this code in functions.php:

function wpa66834_role_admin_body_class( $classes ) {
    global $current_user;
    foreach( $current_user-roles as $role )
        $classes .= ' role-' . $role;
    return trim( $classes );
}
add_filter( 'admin_body_class', 'wpa66834_role_admin_body_class' );

and in custom CSS field:

#targetElement { 
    display: none; 
}
.role-administrator #targetElement { 
    display: visible; 
}

But it doesn't work.

Topic php css Wordpress

Category Web


Solution:

function add_custom_styles() {
    if ( is_admin() ) {  ?>
    <style type="text/css">
        .site-description {
            display: none !important;
        }
        .site-title {
            display: none !important;
        }
    </style>
<?php }
}
add_action('wp_head', 'add_custom_styles');

The code that you've tried already adds the role CSS class to the admin_body_class, which only affects the admin area/backend. Simply change the filter to alter the frontend body class body_class, eg.

function wpa66834_role_admin_body_class( $classes ) {
    global $current_user;
    foreach( $current_user->roles as $role )
        $classes .= ' role-' . $role;
    return trim( $classes );
}
add_filter( 'body_class', 'wpa66834_role_admin_body_class' );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.