Plugin to remove Admin menu items based on user role?

I searched high and low for a plugin that can remove/hide Admin menu items , including custom post types and taxonomies, based on user role.

Every one I have tried only does a global hide, not based on user role. Other more complex ones like adminize do not display custom post types or taxonomies.

Read More

Do I have to write my own function or is there a simple plugin I am overlooking?

Related posts

Leave a Reply

4 comments

  1. Update:

    reading mike’s answer again got me thinking that you can add a new capability to a role and use that as you removal condition, so:

       // first add your role the capability like so
       // get the "author" role object
       $role = get_role( 'administrator' );
    
       // add "see_all_menus" to this role object
       $role->add_cap( 'see_all_menus' );
    
       //then remove menu items based on that
       function remove_those_menu_items( $menu_order ){
                global $menu;
                // check using the new capability with current_user_can
                if ( !current_user_can( 'see_all_menus' ) ) {
                 foreach ( $menu as $mkey => $m ) {
                    //custom post type name "portfolio"
                    $key = array_search( 'edit.php?post_type=portfolio', $m );
                    //pages menu
                    $keyB = array_search( 'edit.php?post_type=page', $m );
                    //posts menu
                    $keyC = array_search( 'edit.php', $m );
    
                    if ( $key || $keyB || $keyC )
                        unset( $menu[$mkey] );
                 }
                }
                return $menu_order;
        }
    
     //Then just Hook that function to "menu_order"
            add_filter( 'menu_order', 'remove_those_menu_items' );
    

    Old answer

    I completely agree with what mike posted but if you’re not up to custom coding
    Take a look at Admin Menu Editor plugin.

    it lets you set access rights by level.

  2. There’s a plugin that created for exact question: Hide Admin Menu. This plugin can:

    • Hide admin menu items based on user role, including Administrator and custom user role
    • Can hide admin bar items, too
    • Can export/import settings to use on other sites

    Another good thing from this plugin is it’s intuitive and easy to use.

  3. Hi @Wyck:

    I can definitely appreciate your needs; I’ve had clients ask for the same. Unfortunately though, WordPress ties admin menu pages (i.e. menu sections) and menu subpages (i.e. menu items) to capabilities, not to roles.

    However it would not be too hard to write some plugin code that allows you to assign admin menus options to roles instead; your script would set the capability required for all menu options to require a new 'not-allowed' capability and then have your code selectively assign the default 'read' capability to every menu option where the current user’s role has the defined capability.

    The bigger question then becomes how to your represent the mapping of user roles to menu options? I could see that requiring a pretty elaborate admin user interface. Of course if you just want to hardcode an array that defines which roles can see what, it’ll be pretty easy…

  4. This will keep only a couple of menu items for non-administrators. @Bainternet’s version uses add_cap, which should be done only once, on theme or plugin activation.

    add_action('admin_menu', function () {
        if (current_user_can('administrator')) {
            return;
        }
    
        /**
         * Keep only specific menu items and remove all others
         */
        global $menu;
        $hMenu = $menu;
        foreach ($hMenu as $nMenuIndex => $hMenuItem) {
            if (in_array($hMenuItem[2], array(
                                             'index.php',
                                             'edit.php?post_type=product',
                                        ))
            ) {
                continue;
            }
            unset($menu[$nMenuIndex]);
        }
    }