Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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');
share|improve this question
    
How about using current_user_can and check by role name? –  Bainternet Apr 10 '11 at 16:12

5 Answers 5

up vote 2 down vote accepted
+50

Why not just add another if?

function remove_menus(){
    global $menu;
    global $current_user;
    get_currentuserinfo();
   //check first user
    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)]);}
        }
    }
    //check second user
    if($current_user->user_login == 'user2'){
        $restricted = array(
                            __('Links'),
                            __('Comments'),
                            __('Appearance'),
                            __('Plugins')
        );
        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');

same function but two different users will get different menus.

share|improve this answer
    
Ah, that's what I was trying to do, but I had the logic wrong. This works fine, thanks. –  songdogtech May 2 '11 at 19:31

Can you not simply add an or into your conditional logic?

if( $current_user->user_login == 'user1' || $current_user->user_login == 'user2' )

Or even better just use an array, so it's easier to update..

Before the if line mentioned above..

$_menu_remove_users = array( 'user1', 'user2' ); 

Then replace the existing if condition with..

if( in_array( $current_user->user_login, $_menu_remove_users ) )

You can then update the array as needed without needing to rewrite your conditional logic.

share|improve this answer
    
Thanks, but I need to show one set of menu items to one user and a different set to a second user, so I need "if else" logic, but I get a "can't redeclare a previously declared" error for the menu code. So I don't know how it's possible. –  songdogtech Apr 17 '11 at 22:59

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

# Add a user to a user group from inside your functions.php file
function test_me( $user_group_A )
{
    $user_group_A[] = 'Take your own name for testing';

    return $user_group_A;
}
add_filter( 'user_group_A', 'test_me' );

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.

share|improve this answer
    
Thanks, that's a nice little plugin. Appreciate your effort, but I want to keep it simple and assign those roles just in functions.php –  songdogtech May 2 '11 at 19:33
    
Ok. You could also simply add the function to your functions.php file and add the test_me()-function after it. –  kaiser May 2 '11 at 20:13

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.

share|improve this answer
    
I want to keep simple role assigments in functions.php –  songdogtech May 2 '11 at 19:31

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.