Pretty self-explanatory. How does one add another user to this function for functions.php, i.e., user2, who will be shown different menu items than user1?
I tried adding another if($current_user->user_login == 'user2')
condition, but no luck. User2 will have different admin rights, if that matters. But basically, I need to be able to show one set of menu items to user1 and another set to user2, so I need to figure out some if else logic. But I tried that, and I get a “can’t redeclare a previously declared” error for the menu function.
function remove_menus()
{
global $menu;
global $current_user;
get_currentuserinfo();
if($current_user->user_login == 'user1')
{
$restricted = array(
__('Links'),
__('Comments'),
__('Appearance'),
__('Plugins'),
__('Profile'),
__('Tools'),
__('Settings')
);
end ($menu);
while (prev($menu)){
$value = explode(' ',$menu[key($menu)][0]);
if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
}
}
}
add_action('admin_menu', 'remove_menus');
Why not just add another if?
same function but two different users will get different menus.
Can you not simply add an
or
into your conditional logic?Or even better just use an array, so it’s easier to update..
Before the
if
line mentioned above..Then replace the existing if condition with..
You can then update the array as needed without needing to rewrite your conditional logic.
Here’s – a small plugin i wrote – that works & is tested.
You can simply upload it and test it using the following function inside your
functions.php
file:How to
Notes
Adding one (or more) users to one of the groups is as easy as shown above. Modifying the menu item groups is nearly the same. Just handle those arrays inside your themes functions.
It looks like your trying to hide certain menu items for certain users, you can do this by making use Roles and Capabilities instead of hard coding usernames and removing menu items. The Capsman plugin allows you to flexible set permissions for admin, editor roles and even define new ones. If a user does not have permissions to see the Settings page, then the admin links will be removed by WordPress.
Also you can use the Plugin Adminimize, with this plugin you can easy hide menus for different roles; also custom roles. maybe its easier as custom code, if you like this.