X

Restrict Admin Menu Items by Username

Snippets by IsItWP

Are you looking for a way to restrict access to menu items based on username? While there’s probably a plugin for this, we have created a quick code snippet that you can use to restrict admin menu items by username in WordPress.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

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');

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 27 best WordPress business themes for your website and how to create a WordPress donation form.

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!
[60% OFF Today!]
[60% OFF Today!]
[10% OFF Today!]
[10% OFF Today!]
[50% OFF Today!]
[50% OFF Today!]