WordPress Admin Menu for Multiple Roles

I have had the need to make users without any regular WordPress capabilities, but I need them to edit/view some things in the admin area. This is easy enough for standard WordPress menu items. But what about custom menus? You can add the menu slug to all of the users that you want to explicitly edit/view this menu. However, editing roles can be bothersome if you either a) have a lot or b) just don't want to mess with capabilities.

I have created a simple way to give a user no regular WordPress capabilities but let them see/manage admin pages.

This methodology assumes that each user has only one role, but it can be modified easily by looping over the $current_user->roles to check for all roles attached to a user.
// create your role(s)
// for more details on this see: https://codex.wordpress.org/Function_Reference/add_role 
// adding "read => true" enables the wp-admin area for the user with just dashboard and profile tabs
    'Custom User Role',
    array( 'read' => true, 'custom_menu_slug' => true )

Now that you have the role, you can now modify your add_menu_page() call to allow this user to view it without giving the user the ability to do anything else

add_action( 'admin_menu', 'add_your_custom_menu_page' );
function add_your_custom_menu_page() {
        global $current_user;
    // now admins and the custom_user_role will see the menu and can work with it
    if ( in_array( $current_user->roles[0], array( 'custom_user_role', 'administrator' ) ) ) {
        // use the dynamic role to register the menu. so as your different users log in, the menu will load for them
                add_menu_page( 'Your Menu Page', 'Your Menu Page', $current_user->roles[0], 'cusotm_menu_slug', 'your_custom_menu_page_contents', '', -1 );
function your_custom_menu_page_contents() {
    // display whatever custom functionality you need....ie forms, tables, etc
Post a comment

Real Time Web Analytics ^