alphabetically order role drop-down selection in dashboard

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.

Leave a Comment