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 MSc of Computer Science.

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...Loading...
Oopps, sorry. This discussion is closed.

If you are a paying customer and you are looking for Tech Support please post a ticket on our Support Tickets page and we will respond promptly.

If you are not a paying customer and you would like to post a pre-sale question or a quote request please use our Contact form.

Why did we close the discussion?

Many of the customers started reporting issues as comments instead of the ticketing system and this started delaying our response time.

We are making this change to serve our customers better. We have ramped up our support team and we are trying to get the reply out on the same day.

Thanks for understanding.

lawbaugh-fidel@mailxu.com couseyrayford@mailxu.com