2

I'm wondering how to change the admin theme based on a user's role. Currently I can change the admin theme using a plugin (ex: Blue Admin) - but I'm not sure how to make those changes based on a role (subscriber etc).

Would also like to have specific menu items / etc shown only for certain roles.

I have no problem diving into functions.php or anything else that may be needed to accomplish this - just hoping to be pointed in the right direction first.

2

You can set force a specific Admin Color Scheme pro user role through a function.
Personally I would first take away the option to select the scheme from profile.php (Back-end Users/Your Profile)

Below is just an example function which does set a specific color scheme for specific user roles.
Please make first make a backup of the functions.php before adding this function.

/**
 * Set Admin Color Scheme by Role
 * Codex:   {@link https://codex.wordpress.org/Roles_and_Capabilities}
 *          {@link https://codex.wordpress.org/Function_Reference/wp_get_current_user}
 * @version WordPress 4.6 
 */
add_filter( 'get_user_option_admin_color', 'wpse_238039_set_admin_color' );
function wpse_238039_set_admin_color()
{
    $current_user = wp_get_current_user();

    // Check for the user role
    if ( user_can( $current_user, 'subscriber' ) )
    {
        // Remove the Admin Color Scheme picker
        remove_action( 'admin_color_scheme_picker', 'admin_color_scheme_picker' );

        // Set the Admin Color Scheme you want for this role
        return 'light';
    }

    if ( user_can( $current_user, 'contributor' ) )
    {
        remove_action( 'admin_color_scheme_picker', 'admin_color_scheme_picker' );
        return 'coffee';
    }

} // end function

It is of course possible to leave the Admin Color Scheme option on the user Profile Page by removing those lines from the function.

Would also like to have specific menu items / etc shown only for certain roles.

It is possible to add/remove items within another function with the same kind of IF statement blocks. Just be aware of what you do/want in a specific function, and use the correct hooks

Note: see the @link urls in the function above for references.

  • Really nice layout of your code. Simple, to the point, and you even took the liberty to hide the admin color scheme settings as well, +1. – Ethan Jinks O'Sullivan Sep 4 '16 at 3:41
0

The admin color scheme is stored in user_meta as admin_color (default is 'fresh').

You could write a callback that fires on user_registration that would set it depending on role.

add_action( 'user_register', 'wpse_registration_save', 10, 1 );

function wpse_registration_save( $user_id ) {
   # ...get the user role
   # ...write switch statement based on role
   # ... update_user_meta($user_id, 'admin_color', 'funky');

}

I'm not sure how this plays with Blue Admin, but this would be the way to do it without using any plugins. If it's smart, Blue Admin would store its theme info in user_meta, too, so it might be a very similar approach either way.

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.