WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When writing WordPress plugins there is often a need to set up options for which roles on the site have access to certain functionality or content. To do this a plugin dev needs to fetch the list of roles that exist on the site to use in the option. Because custom roles can be created we cannot assume the default roles are the only ones available.

What is the best way to fetch the list?

share|improve this question
Note: This question was originally asked by Ryan Bilesky on the wp-hackers mailing list and answered by me. I added it here because it's a clear case of best-practice question that people might ask google :) – jeremyclarke Sep 11 '10 at 19:42
Hey @Jeremy Clarke - Really glad to have your participation here and these are exactly the types of things we are looking for here; answers related to common questions that can also reveal best practices. Kudos! – MikeSchinkel Sep 11 '10 at 23:07
up vote 28 down vote accepted

Roles are stored in the global variable $wp_roles.

The ideal function is get_editable_roles() from /wp-admin/includes/user.php

function get_editable_roles() {
    global $wp_roles;

    $all_roles = $wp_roles->roles;
    $editable_roles = apply_filters('editable_roles', $all_roles);

    return $editable_roles;

The "editable" part is because it offers other plugins a chance to filter the list in case someone other than admin has 'edit_users' privilege (and thus 'admin' needs to be removed from the list, else that user could make themselves admin). Role management plugins used to create custom roles are the ones that would be using that filter. Otherwise this function is essentially get_roles() (which doesn't exist).

Presumably your plugin will only offer the settings page in question to someone who has admin-level capabilities like 'manage_options' and is basically an admin with access to all roles, so the filter shouldn't affect you.

There is also wp_dropdown_roles() which gives you the roles as <option> fields for a <select> list (though checkboxes are likely to work better in many scenarios where you're choosing who has access to something).

share|improve this answer
Note: I created this function as part of a patch to make WP more secure, so I'm biased in its favor ;) – jeremyclarke Sep 11 '10 at 20:06

Try this:

function get_role_names() {

global $wp_roles;

if ( ! isset( $wp_roles ) )
    $wp_roles = new WP_Roles();

return $wp_roles->get_names();

PS heh, missed that explanation and reply, too fast me :)

share|improve this answer

Here is how you can find the list of roles without any plugins or function http://screencast.com/t/uaWsGLAR3Sh

share|improve this answer
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Gabriel Jan 26 at 21:19
That's a pretty straightforward approach to simply get a list of user role values! Thanks @Rarst – Jarmerson May 5 at 20:10

For those which have multilingual site, function

function wp_roles_array() {
    $editable_roles = get_editable_roles();
    foreach ($editable_roles as $role => $details) {
        $sub['role'] = esc_attr($role);
        $sub['name'] = translate_user_role($details['name']);
        $roles[] = $sub;
    return $roles;

returns localized array like this (role names are in Slovak language):

    [0] => Array
            [role] => administrator
            [name] => Administrátor

    [1] => Array
            [role] => editor
            [name] => Editor

    [2] => Array
            [role] => author
            [name] => Autor

    [3] => Array
            [role] => contributor
            [name] => Prispievateľ
share|improve this answer

Your Answer


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.