X

Restrict admin menu items by username

Adding this PHP code to the functions.php of your wordpress admin will allow you to restrict access to menu items based on username.


function remove_menus()
{
    global $menu;
    global $current_user;
    get_currentuserinfo();

    if($current_user->user_login == 'username')
    {
        $restricted = array(__('Posts'),
                            __('Media'),
                            __('Links'),
                            __('Pages'),
                            __('Comments'),
                            __('Appearance'),
                            __('Plugins'),
                            __('Users'),
                            __('Tools'),
                            __('Settings')
        );
        end ($menu);
        while (prev($menu)){
            $value = explode(' ',$menu[key($menu)][0]);
            if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
        }// end while

    }// end if
}
add_action('admin_menu', 'remove_menus');

Comments  Leave a Reply

  1. Hi, do you have a method to hide a specific plugin’s menu and allow others? I Need to restrict a user (DB_manager) to only be able to manage Form Database plugin and nothing else on the Admin panel.

  2. Thanks, but it doesn’t seem to work for custom post types. Any suggestions?

  3. Seen this before. Pretty useful except doesn’t seem to work with menus that include spaces such as:
    __(‘Some Menu’)
    I’ve tried a few different special characters like underscores, nonbreaking space, etc. but doesn’t seem to do the trick. Also adjusted the priority, but it seems that this function doesn’t recognize spaces in the array. Any ideas?

    1. Hi Bryan,

      You may want to use this method
      http://codex.wordpress.org/Function_Reference/remove_menu_page
      You could then use “remove_menu_page” and “remove_submenu_page”

      you could then lose the while, something like: http://pastebin.com/8ShwZ5rg
      I have not tested this but should work.

  4. This snippet is only hide menu items from screen but doesn’t restrict access by url.

  5. any way to strip out a plug in admin feature, specifically eShop, from the Dashboard Admin.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!