0

How can I apply custom CSS for user role, both for dashboard and frontend? For dashboard I set:

if( current_user_can('seller', 'partner')) { 
    function admin_style() {
  wp_enqueue_style('admin-styles', get_template_directory_uri().'/admin.css');
}
add_action('admin_enqueue_scripts', 'admin_style');
}
   else {

}

how to modify the code to load also on front? btw, Is if( current_user_can('seller', 'partner')) correct for checking is user role is seller and partner?

1

To enqueue CSS in WordPress you can use:

  • wp_enqueue_scripts action for the frontend
  • login_scripts action for the login page
  • admin_scriptsaction for the admin side, as you already know

To check user's roles you should get the user object and check the roles property. current_user_can() is function intended to check capabilities, not roles.

So, to add CSS based on user roles in the frontend:

add_action( 'enqueue_scripts', 'cyb_enqueue_styles' );
function cyb_enqueue_styles() {

    $user = wp_get_current_user();
    if( ! empty( $user ) && count( array_intersect( [ "seller", "partner" ], (array) $user->roles ) ) ) {
        wp_enqueue_style( 'my-style', get_template_directory_uri().'/some-style.css' );
    }

}
| improve this answer | |
  • How can I check if one user has one particular role and doesn't have another using the same logic? In this case to check if user is partner but is not seller? Thank you for answer! – Rafal Nov 1 '16 at 1:24
  • What problem do you have to do It? I don't undersntand you. In the answer you can see how to get the current user roles, checking if the user has or has not some role should be trivial, can you explain deeper the problem you have? – cybmeta Nov 1 '16 at 14:16
  • Sorry, I didn't see the whole answer. Everything is fine now. Thank you – Rafal Nov 2 '16 at 9:01

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.