A fresh mix of code snippets and tutorials

WordPress: Get All User Roles

WordPress makes it easy to get the current user’s role(s) by using wp_get_current_user(). But what if you want to get all roles from all users. Like a list of every role that is used on your site. Well good news, WordPress provides a global object named $wp_roles that provides the information. So if you’re looking for a way to get a list of all currently available roles on a WordPress site, here’s how to do it.

Get all user roles

Here is an example of how to use $wp_roles in a function:

function shapeSpace_get_roles() {
	global $wp_roles;
	$roles = null;
	if ($wp_roles && property_exists($wp_roles, 'roles')) {
		$roles = $wp_roles->roles;
	return $roles;

Then you can call it like so:

<?php $all_roles = shapeSpace_get_roles(); ?>

And BOOM! You now have an array of all the roles. No modifications are required. The simple get-roles function does what it says on the tin. Nothing more, nothing less.


An easier way to get the user roles is to use the WordPress function, get_editable_roles().

Tip: if you get an error when using get_editable_roles(), add the following line of code before calling, like this:

if (!function_exists('get_editable_roles')) require_once ABSPATH .'wp-admin/includes/user.php';

$roles = get_editable_roles();

I use this technique in my free WordPress plugin, Simple Login Notification. Check out the source code to view an example in context.

See also:

Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress