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

Related posts

Leave a Reply

5 comments

  1. 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.

  2. 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.

  3. 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.

  4. 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.