Revoke Access to certain user roles and admin pages

You can do this several ways, it just depends on how specific you want to get. Like do you need to disable for only one user with the ID of 21 or was that just an example? And are you trying to disable several pages or just a couple?


Example 1: You can do this to redirect the dashboard to the edit posts page if not an administrator.

add_action('load-index.php', function(){
   if(!current_user_can('administrator')){
      if(get_current_screen()->base == 'dashboard')
         wp_redirect(admin_url('edit.php'));
   }
});

Example 2: Or for a lot of pages you could do something like this:

function bw_redirect_admin_pages() {
    if(!current_user_can('administrator')){
      global $pagenow;
      $admin_pages = array(
                                'options-writing.php',
                                'options-reading.php',
                                'options-discussion.php',
                                'options-media.php',
                                'options-privacy.php',
                                'options-permalink.php',
                        );
      if(in_array($pagenow, $admin_pages)){
        wp_redirect( admin_url("https://wordpress.stackexchange.com/") ); 
        exit;
      }
   }
}
add_action('admin_init', 'bw_redirect_admin_pages');

Sidenote : If you need to be more specific on users you can use wp_get_current_user()


Example 3: You can also change individual capabilities for other users.

Let’s say your user ID is 1. Now you can use map_meta_cap and block certain capabilities from users who aren’t you. So in this example below, your saying that if the user is not user 1 (or whatever your ID is) than they can’t delete or edit users.

add_filter('map_meta_cap', 'prevent_user_edit', 10, 4 );
function prevent_user_edit( $required_caps, $cap, $user_id, $args ){
    $protected_user = 1; // ID of user not editable (this should be YOUR user id)
    if ( $user_id === $protected_user ) // Don't block caps if current user = protected user
        return $required_caps;
    $blocked_caps = array(
        'delete_users',
        'edit_users'
        );
    if ( in_array( $cap, $blocked_caps ) && $args[0] === $protected_user )
        $required_caps[] = 'do_not_allow';
    return $required_caps;
}

Each admin menu and submenu is associated with a specific capability for different roles. So in the above example we disallow all users beside yourself to access the users page and also disallow them to delete users.

See the Roles vs Capability table to decide what to block as well as the menus associated with those capabilities here.

This is much safer than just “redirecting a page”. The example above is a good demonstration of this. If only redirecting people away from the “users” page on page load, they’d still have the delete_users capability which you don’t want.

You could then combine this example with one of the redirect ones if you wanted them to also redirect.


Example 4: Or another way to do the same thing above but with your email instead of your ID:

add_filter( 'user_has_cap',
function( $caps, $cap, $args ) {
    $user_id = $args[1];
    $user = new WP_User( $user_id );
    $email = $user->user_email;
    if ( $email != get_option('admin_email') )
        $caps['delete_user'] = false;
        $caps['edit_user'] = false;
    return $caps;
}, 10, 3 );

Here are some of the more common capabilities you can block

Example 5: I highly suggest giving 20 people administration roles though. You’d be much safer creating a custom role:

add_action( 'after_switch_theme', 'create_new_user_role' );

function create_new_user_role() {

    $add_role = add_role( 'manager', __( 'Site Manager' ),
            array(
                'read' => true,
                'edit_posts' => true,
                'edit_pages' => true,
                'edit_others_posts' => true,
                'create_posts' => true,
                'manage_categories' => true,
                'publish_posts' => true
            ));
}

Example 6: Change Displayed Role name

If they absolutely must be have “administrator” role, you could do some trickery. I haven’t actually tested this though.

function brw_change_administrator_role_name() {
    global $wp_roles;
    if ( ! isset( $wp_roles ) )
        $wp_roles = new WP_Roles();

    // Change administrator role name to developer
    $wp_roles->roles['administrator']['name'] = 'Developer';
    $wp_roles->role_names['administrator'] = 'Developer';  

    // Change editor role name to administrator
    $wp_roles->roles['administrator']['name'] = 'Administrator';
    $wp_roles->role_names['administrator'] = 'Administrator';  


}
add_action('init', 'brw_change_administrator_role_name');

Similarly, you could also try this (although again not tested):

add_filter(  'gettext',  'brw_translate_words_array'  );
add_filter(  'ngettext',  'brw_translate_words_array'  );
function brw_translate_words_array( $translated ) {

     $words = array(
                        'Administrator' => 'Developer',
                        'Editor' => 'Administrator'
                    );
     $translated = str_ireplace(  array_keys($words),  $words,  $translated );
     return $translated;
}