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.
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.
Thanks, but it doesn’t seem to work for custom post types. Any suggestions?
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?
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.
This snippet is only hide menu items from screen but doesn’t restrict access by url.
any way to strip out a plug in admin feature, specifically eShop, from the Dashboard Admin.
This method may work better for you,
http://codex.wordpress.org/Function_Reference/remove_menu_page