Search code examples
phpwordpressfilteruser-roleswordpress-admin

Trying to reverse the order of user roles found in selector in WordPress user section


Trying to change the order of the user role selector so 'Administrator' role is at top of selector with other custom roles below. Reversing the order would work. Or a DESC order .vs ASC. The selector I'm referring to is found in dashboard -> user.

I've tried working with this code: https://wordpress.stackexchange.com/questions/74785/alphabetically-order-role-drop-down-selection-in-dashboard but I'm struggling in achieving a reverse order (I don't need an alphabetical list of options)

I have tried this, but I'm failing:

add_filter('editable_roles', function($roles){

  usort($roles, function($a, $b){
    return $a["name"] < $b["name"]?1:-1);
  });   

  return $roles;
});

I am able to move Administrator to the top of the list, but the other three roles/options do not change or reverse their order. Any help would be much appreciated!! I have three custom roles in addition to Administrator if that has any bearing.

Update: Here's an image of the roles. I am trying to reverse the order where Administrator is on top, and Read Only is on the bottom: enter image description here


Solution

  • If I understand you correctly and really all you want to do is to simply reverse the order of the $roles array, then just do this:

    add_filter('editable_roles', function ($roles) {
        $roles = array_reverse($roles);
        return $roles;
    });
    

    Function References: