How to add another user to this remove_menu function?

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');

5 Answers
5

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.

Leave a Comment