Remove Dashboard Menus for Specific Users

For a while now I have been using some simple code to remove the dashboard menus in WordPress, primarily so that clients can’t change certain settings and ‘break’ their sites. However the code I was using also removed those items for me too. This meant I had to memorise the admin URLs and visit them manually which is not ideal. Today I have managed to solve this problem by hiding WordPress dashboard menus for all except specified users.

The trick is to wrap your code around an if statement that first checks who the user is. The code I am using is below:

https://gist.github.com/wpmark/8160972

Basically the code grabs the current users user ID and then compares it to a list of allowed users. If is matches the function returns nothing, if it doesn’t match then it removes the specified menus in the WordPress admin areas.

In this example users with the ID 1 and 2 can see all menus, however the WordPress links menu will be removed for all other users. It is important to note that this code only removes the menus from view and doesn’t stop users from actually viewing these pages. If they know the URLs they will still load.

9 responses

  1. where I can paste that code?

  2. Paste it into your themes functions.php file.

  3. Giuseppe Avatar

    Hi, for the 3.5 wordpress version is valid?

    1. Yes still working for me.

  4. William Avatar

    Hi, love this Function. Is there away to reverse it? So that the menus are hidden to just the ID you add?

    So everyone can see everything except the users you add

    1. Remove the else part of the code and add your ids to replace 1 and 2.

      1. William Avatar

        thank you, so it would look like this.

        add_action( ‘admin_menu’, ‘pj_remove_menus’, 999 );
        function pj_remove_menus() {
        global $current_user; $current_user = wp_get_current_user(); $current_user_id = $current_user->ID; // get the user ID
        if($current_user_id == ’16’ || $current_user_id == ’14’ || $current_user_id == ’15’) {
        remove_menu_page( ‘link-manager.php’ );
        }
        }

        Where would i find a complete list of the menu names and urls to add to this?

  5. William Avatar

    Can this be set up in reverse? So that it hides the menu for a specific id and shows them for everyone else?

Leave a Reply

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