WP Mayor is supported by its audience. Our recommendations are based on our experts’ research and hands-on testing. If you purchase through a link on our website, we may earn a commission. Here’s why you can trust us.

How to Remove Menu Items in Admin Depending on User Role

  Introduction
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 Top Picks

After reviewing and testing the best solutions, WP Mayor recommends:

WP Mayor is supported by its audience. Our recommendations are based on our experts’ research and hands-on testing. If you purchase through a link on our website, we may earn a commission. Here’s why you can trust us.
 Table of contents

So let’s 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 your guest posters. How do you hide them?

Remove Menu Items in Admin

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 the 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:

Remove the Ability To See Certain Posts

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:

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 an investor, entrepreneur, and blogger. He is the founder of WP Mayor, the plugins WP RSS Aggregator and Spotlight, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.
Get more expert advice like this delivered to your inbox every week.
Join thousands of designers, developers, and marketers who trust our experts to find and test the best tools and services for your website.
By signing up to our newsletter, you agree to receive electronic communications from WP Mayor that may include advertisements.
Get more expert advice like this delivered to your inbox every week.
Join thousands of designers, developers and marketers who trust our experts to find and test the best tools to build, improve, and optimize their websites.
By signing up to our newsletter, you agree to receive electronic communications from WP Mayor that may include advertisements.
Join the Conversation

35 Responses

  1. 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.

  2. 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?

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

  4. 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.

  5. 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.

  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.

    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…

  7. 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

  8. 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.

  9. 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

  10. 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

      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

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

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

  12. very useful, thank you!

    Any idea how I could hide other users images/media from a user? The code above only seems to work for posts…

Leave a Reply

Your email address will not be published.

From web hosting to WooCommerce plugins and backup services, we have put together a collection of exclusive coupons and deals just for you.
WP Mayor_Icon_white

Reader Survey

Supercharge your WooCommerce store!