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.
where I can paste that code?
Paste it into your themes functions.php file.
Hi, for the 3.5 wordpress version is valid?
Yes still working for me.
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
Remove the else part of the code and add your ids to replace 1 and 2.
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?
Something like this would probably be best:
http://snippi.com/s/41j7x3k
Also take a look here on the codex for help:
http://codex.wordpress.org/Function_Reference/remove_menu_page
http://codex.wordpress.org/remove_submenu_page
Can this be set up in reverse? So that it hides the menu for a specific id and shows them for everyone else?