Getting a List of Currently Available Roles on a WordPress Site?

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).

Leave a Comment

deneme bonusu veren sitelerbahis casinomakrobetceltabetpinbahispolobetpolobet girişpinbahis girişmakrobet girişpulibet girişmobilbahis girişkolaybet giriş