How To Disable WordPress Admin Menu For Specific ‘Admin’ Users

Disable-WP-Admin-Menu

As a part of WordPress customization for clients, we generally follow a simple exercise. We create two admin accounts for the client. One account has all the options to play around with (all the privileges an administrator generally has) and the other account provides access to the options needed for managing the site on the day to day basis, like creating pages, posts, categories, etc while hiding items like plugins, tools, comments, links, etc.

This brings us to the quick handy tip — disable admin menus for specific admin users. This will help you replicate the super-admin concept of Joomla and Drupal; only one admin user has all the rights while others have limited access to the admin menu.

In the process of removing the menus, you can individually remove top-level menus or remove all the menus at once or remove just the specific submenu items.

Remove Top-level Menus (Individually)

WordPress offers a built in function remove_menu_page($menu_slug) for removing a top-level admin menu.

Usage

Use the following code to remove the Users menu:

remove_menu_page(users.php);

Remove Sub-menu Items

Use the function remove_submenu_page( $menu_slug, $submenu_slug ) to remove a sub-menu item.

Usage

Use the following code to remove the Categories sub-menu:

remove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=category' );

Remove Multiple Menus Items

You can hide multiple menu items all at once, create an array and pass the name of the menus you want to hide.

Usage: To hide Dashboard, Media, Links, Appearance, Tools, Users and Settings all at once paste the following code in functions.php:


function remove_admin_menus () {
    global $menu;
    $restricted = array(__('Dashboard'), __('Media'), __('Links'), __('Appearance'), __('Tools'), __('Users'), __('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_admin_menus');

Now that you know the various functions used to remove / hide / disable admin menus, let’s club these code nuggets to implement the super-admin functionality to WordPress.

Here we check the current user id and if the user id is other than 1, the default admin, we can hide the required admin menus. Copy and paste the following code in your theme’s functions.php [you can customize the code to remove specific menu items if needed].


function remove_menus () {
$uid = get_current_user_id();
    if($uid == 1) return;
    global $menu;
    // check if admin and hide these for admins
    if( (current_user_can('install_themes')) ) {
        $restricted = array(
           __('Dashboard'),
           __('Links'),
            __('Appearance'),
            __('Tools'),
            __('Users'),
            __('Settings'),
            __('Comments'),
            __('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)]);
            }
        }

remove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=category' );
remove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=post_tag' );
Divi WordPress Theme