Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

The site I'm working on will have a very large number of unique user roles via a members plugin. Because of that, I'd like to have the Role drop-down selection on the Add New User page to display roles alphabetically, rather than descending order of creation.

Is there any way to do this?

share|improve this question
add comment

2 Answers

up vote 7 down vote accepted

Almost the same approach One Trick Pony has chosen, but I am using translated names and uasort() (to preserve the keys):

add_filter( 'editable_roles', 't5_sort_editable_roles' );

/**
 * Array of roles.
 *
 * @wp-hook editable_roles
 * @param   array $roles
 * @return  array
 */
function t5_sort_editable_roles( $roles )
{
    uasort( $roles, 't5_uasort_editable_roles' );
    return $roles;
}
/**
 * Compare translated role names.
 *
 * @param  array $a First role
 * @param  array $b Second role
 * @return number
 */
function t5_uasort_editable_roles( $a, $b )
{
    return strcasecmp(
        translate_user_role( $a['name'] ),
        translate_user_role( $b['name'] )
    );
}

As a plugin on GitHub.

share|improve this answer
    
Awesome, works perfectly. Thanks toscho. –  Chirico Dec 3 '12 at 17:05
    
@Chirico See my update, the first version was buggy. –  toscho Dec 3 '12 at 17:13
    
Thanks again, I just started running into problems with the first version. looks like things are as they should be now. –  Chirico Dec 4 '12 at 4:10
add comment

Apparently there's a filter that you can use in the get_editable_roles function (which is called in that page):

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

  // sort alphabetically (ignores case)
  usort($roles, function($a, $b){
    return strcasecmp($a["name"], $b["name"]);
  });   

  return $roles;
});
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.