Home > Enterprise >  Wordpress how to hide user role tab from users list
Wordpress how to hide user role tab from users list

Time:12-25

I have already hidden the admin user from the list and subtracted the users number, however, the administrator role tab from the users list ( Users >> All Users ), is still shown, and I would like to hide this as will.

This is the code I used to hide admin:

add_action('pre_user_query','site_pre_user_query');
function site_pre_user_query($user_search) {
    global $current_user;
    $username = $current_user->user_login;
 
    if ($username == 'admin') {
    }
 
    else {
    global $wpdb;
    $user_search->query_where = str_replace('WHERE 1=1',
      "WHERE 1=1 AND {$wpdb->users}.user_login != 'admin'",$user_search->query_where);
  }
}

And this is what I used to subtract the users numbers:

add_filter("views_users", "site_list_table_views");
function site_list_table_views($views){
   $users = count_users();
   $admins_num = $users['avail_roles']['administrator'] - 1;
   $all_num = $users['total_users'] - 1;
   $class_adm = ( strpos($views['administrator'], 'current') === false ) ? "" : "current";
   $class_all = ( strpos($views['all'], 'current') === false ) ? "" : "current";
   $views['administrator'] = '<a href="users.php?role=administrator" >' . translate_user_role('Administrator') . ' <span >(' . $admins_num . ')</span></a>';
   $views['all'] = '<a href="users.php" >' . __('All') . ' <span >(' . $all_num . ')</span></a>';
   return $views;
}

Thanks

enter image description here

CodePudding user response:

"I am not trying to hide the user, am trying hide the ( Administration ) tab on the users list"

You could use unset function and views_users filter hook to remove the "administrator role tab".

add_filter("views_users", "removing_admin_tab_from_users_list");

function removing_admin_tab_from_users_list($views)
{
    unset($views['administrator']);
    return $views;
}

And here's the result:

enter image description here

  • Related