Search
Close this search box.

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?
Table of Contents

Sponsored Ad

If you purchase through a link on our site, we may earn a commission.

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

Sponsored Ad

If you purchase through a link on our site, we may earn a commission.

All suggestions are anonymous.

More from our blog...

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.

    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.

    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. Nice one! I’m using easy-admin-menu plugin, Allows you to reorder and hide items in the menu with a very easy interface, just drag and drop. Might be useful

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

  13. How do you remove plugin links like this:

    I have tried “remove_menu_page( ‘admin.php?page=wpfilebase_sets’ );” but that didn’t do anything

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

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

  15. Hi Jean,
    I would like to say the way you illustrated will definetly going to help lots of blogger in terms of how to remove the menu items. Nice Coding.

    View my recent post on

    Regards
    Siddhartha Sinha

Post a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Stay updated with WP Mayor's newsletter showcase every week

Stay on top of every new WordPress innovation and latest launches. Receive all our fresh product reviews and expert guides directly in your inbox.

Hosting Survey 2024

Are you happy with your hosting provider or are you over-paying for too little? Have your say below!

"*" indicates required fields

What's the main reason you picked this host?*
How happy are you with your host?*

OPTIONAL: If you'd like to receive the results of this survey, please enter your details below.