How to Remove Menu Items in Admin Depending on User Role

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?
Our Sponsors
Essential plugins for your WooCommerce store
Build unbuildable WordPress websites
The best no-code Instagram feed plugin for WordPress

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?

Turns out it’s quite simple, just take a look at the url of those menu items, and then use that to build up your function like so:

[php]
add_action( ‘admin_init’, ‘my_remove_menu_pages’ );
function my_remove_menu_pages() {

global $user_ID;

if ( current_user_can( ‘wpmayorauthor’ ) ) {
remove_menu_page( ‘edit.php?post_type=thirstylink’ );
remove_menu_page( ‘edit.php?post_type=wprss_feed’ );
remove_menu_page( ‘authorhreview’ );
}
}
[/php]

Here are some of menu page names for the most common menu items that come with WordPress:

[php]
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
[/php]

There are also some plugins that can help you out if you prefer that route over coding yourself:

Adminimize

Admin Menu Editor

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:

[php]
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;
}
}

}
[/php]

And of course, if you haven’t already heard about it, here’s an excellent plugin for managing roles and capabilities in WordPress:

Members by Justin Tadlock

Also, if you’re looking for a managed host with knowledgeable support which you can contact 24/7, have a look at SiteGround. They offer WordPress-specific support and their agents can help you with inquiries regarding plugins, themes and other application-related issues.

If you enjoyed this post, make sure to subscribe to WP Mayor’s RSS feed.

Jean Galea
Jean Galea
Jean Galea is a WordPress developer, entrepreneur and padel player. He is the founder of WP Mayor, the plugins WP RSS Aggregator and EDD Bookings, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.

Consider sharing this post so others can find it:

Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on telegram
Share on whatsapp
Share on pocket
Share on email

Join thousands of people receiving real-world, genuine evaluations of WordPress products and services just like this one every week.

Table of Contents

35 Responses

      1. didn’t work for me.. remove_menu_page is not working in my case… Will you please help me out..

  1. Great another thread that not helping, i want to remove tools from admin menu…not using functions but just delete it….

      1. very cool…. any idea how to force the particularly stubborn plugins that refuse to vanish to disappear? Maybe some way to “hide” them? That’s what the menu plugins have to do for these particular items. Thanks

  2. the solution is to just put the very last bit of the url so instead of remove_menu_page( ‘edit.php?post_type=thirstylink’ ); you would put remove_menu_page( ‘thirstylink’ ); – hope that helps someone

  3. Many thanks for sharing this, very useful.

    I still have a problem with removing an item from the dashboard top menu line, which seems to fall exactly into the group of pages what I can remove from the sidebar:
    …wp-admin/admin.php?page=wpseo_dashboard
    I can’t figure out why it refuses to disappear.
    I’d appreciate an advice

  4. Hello! What a great bit of code, especially useful for us was hiding posts from other authors in the list.

    How about multiple authors of a post, all being able to see that post in their respective lists?

    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;
    }
    }

    }

    With the original code, even after adding an author to a post (now post has two authors) the post does not show up in the second, additional authors, post list.

    Only the first and original author of a post is able to see the post (which now has multiple authors.)

    Using the Co-Authors Plus plugin to add multiple authors to a post. https://wordpress.org/plugins/co-authors-plus/

    In this article, it talks about how Co-Authors plugin stores the terms… but I’m not sure how one would extract those terms and use them with the wp_redirect, to list more than one author. http://wordpress.stackexchange.com/questions/5871/how-to-display-an-archive-with-multiple-authors

    Hope that makes sense : ) thank you for any advice.

  5. Thanks your code does work! But when I add condition
    if ( current_user_can( ‘wpmayorauthor’ ) ) {…}

    to my child theme not to the main theme it does not work. I think it does not recognize the current_user_can function. /themename-child/function.php any ideas why? Do I need to somehow do an include so the current_user_can function is recognized?

    Thanks

    1. Hi Laura, in what way? This is about the WP Admin menu. For the menu on your frontend you can just use the WP Menu settings…

  6. This code didn’t work for me with WP 4.5.4
    I have a custom user role that I created earlier so I have…

    add_action( ‘admin_init’, ‘remove_menu_pages’ );
    function remove_menu_pages() {

    global $user_ID;
    if( current_user_can(‘client’) ) {
    remove_menu_page(‘plugins.php’);
    }
    }

    …but the plugin menu item continues to show. Would really like to figure out what’s wrong here.

  7. Hi! can anyone tell me which file should I edit? I can’t try it out since I don’t know which PHP should I mess with. Thanks!

    1. Francisco, usually for this kind of thing, you would do it in the themes’ functions.php file. If your theme doesn’t have that file, simply create it withing the theme folder.

  8. Hi,
    I am unable to remove the menu pages created by the acf plugin. I am using the code remove_menu_page( ‘edit.php?post_type=acf’ ).
    Can you please help me out.

  9. Your code for users to only see their own post might be problematic. The user can simply change the author parameter in the url!

  10. This is by far the best and easiest solution to implement. No plugins, no configurations, just a few lines of code.

    Can this be also targeted to specific usernames or userIDs and not only to a role?

  11. This works a treat! Thanks 🙂

    To do this for a custom role (created in the Members plugin) my code is:

    $user = wp_get_current_user();
    if ( $user->roles[0] == ‘cattery_admin’ ){
    remove_menu_page( ‘edit.php’ );
    remove_menu_page( ‘edit-comments.php’ );
    remove_menu_page( ‘tools.php’ );
    }

    You may ask: Why do I need this if I am using the Members plugin?

    Well, under Media in Members the only option is to Upload files. To edit image attributes or to delete uploaded images, I enabled the default capability under Posts to Edit and Delete Posts. This then shows the menu items of Posts, Comments, and Tools, that I don’t want for this role… hence the above code.

Leave a Reply

Your email address will not be published. Required fields are marked *

The Beginner’s Handbook
From an introduction on how WordPress works to our recommendations on products and services.

Join thousands of people receiving real-world, genuine evaluations of WordPress products and services

A single weekly email directly to your inbox.