Home / admin / Restrict admin menu items by username
  • How to Restrict admin menu items by username

    no thumbnail sorry: Restrict admin menu items by username

    Adding this PHP code to the functions.php of your wordpress admin will allow you to restrict access to menu items based on username.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    function remove_menus()
    {
        global $menu;
        global $current_user;
        get_currentuserinfo();
        if($current_user->user_login == 'username')
        {
            $restricted = array(__('Posts'),
                                __('Media'),
                                __('Links'),
                                __('Pages'),
                                __('Comments'),
                                __('Appearance'),
                                __('Plugins'),
                                __('Users'),
                                __('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)]);}
            }// end while
        }// end if
    }
    add_action('admin_menu', 'remove_menus');
    

    ( WordPress codex functions, hooks, in this snippet. )

    admin_menu, get_currentuserinfo, add_action, __,
  • Zodoria

    any way to strip out a plug in admin feature, specifically eShop, from the Dashboard Admin.

  • This snippet is only hide menu items from screen but doesn’t restrict access by url.

  • Bryan

    Seen this before. Pretty useful except doesn’t seem to work with menus that include spaces such as:
    __(‘Some Menu’)
    I’ve tried a few different special characters like underscores, nonbreaking space, etc. but doesn’t seem to do the trick. Also adjusted the priority, but it seems that this function doesn’t recognize spaces in the array. Any ideas?

  • Valeriu Vetiul

    Thanks, but it doesn’t seem to work for custom post types. Any suggestions?

  • RowBe

    Hi, do you have a method to hide a specific plugin’s menu and allow others? I Need to restrict a user (DB_manager) to only be able to manage Form Database plugin and nothing else on the Admin panel.