Remove menu items in wp-admin depending on user role
December 7, 2015 in Code Snippets by
So lets say you open up your blog to guest posters, and you create a user for them, and possibly even a custom role.
You will usually find one or more menu items that you don’t want to show to your guest posters. How do you hide them?
Just take a look at the url of those menu items, and then use that to build up your function like this:
/* Remove Tools admin menu item for everyone other than Administrator */ add_action( 'admin_init', 'remove_menu_pages_for_all_except_admin' ); function remove_menu_pages_for_all_except_admin() { global $user_ID; if ( !current_user_can('administrator') ) { remove_menu_page('tools.php', 'tools.php'); } }
Here are some of menu page names for the most common menu items that come with WordPress:
remove_menu_page('edit.php'); // Posts remove_menu_page('upload.php'); // Media remove_menu_page('link-manager.php'); // Links remove_menu_page('edit-comments.php'); // Comments remove_menu_page('edit.php?post_type=page'); // Pages remove_menu_page('plugins.php'); // Plugins remove_menu_page('themes.php'); // Appearance remove_menu_page('users.php'); // Users remove_menu_page('tools.php'); // Tools remove_menu_page('options-general.php'); // Settings
Here’s one other bonus tip for you. If you want to remove the ability of a role to see the list of posts by other users, use this code:
add_action( 'load-edit.php', 'posts_for_current_contributor' ); function posts_for_current_contributor() { global $user_ID; if ( current_user_can( 'contributor' ) ) { if ( ! isset( $_GET['author'] ) ) { wp_redirect( add_query_arg( 'author', $user_ID ) ); exit; } } }

About Milan
The founder of Dessky, Milan has worked in all aspects of advanced web development, from building large commercialized e-commerce and social network systems to troubleshooting small wordpress blogs. His extensive skills cover virtually every area of web development. Milan works hard to implement tomorrow’s trends utilizing the cutting edge systems of today. He specializes in rich internet web application development and deployment, complex HTML5/CSS3 graphical design layouts, full blown framework-driven rich internet applications, and much more. Milan provides elegant solutions to complex problems encountered by businesses that use internet based services. Also he is the Graduated Engineer of both Computer Science and Information Technology.