Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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

share|improve this question
    
Try this plug in: [Hide Admin Panels](wordpress.org/extend/plugins/hide-admin-panels.. worked for me. :)) –  user5836 May 31 '11 at 17:39

4 Answers 4

up vote 7 down vote accepted

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.

share|improve this answer
    
I tried this but the hide feature seems to be a global setting, its on/off , no user role control. –  Wyck Jan 30 '11 at 5:18
    
Ok, new approach, i updated my answer. –  Bainternet Jan 30 '11 at 9:49

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.

share|improve this answer

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...

share|improve this answer
    
Thanks Mike, maybe an idea for a plugin if time permits, but for now I think i'll just do something simple and hardcode an array, funny thing is I posted this in the functions thread on here a few weeks ago. –  Wyck Jan 30 '11 at 5:23

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]);
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.