WP-Mix

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 = $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.

See also:

Learn more

WordPress Themes In Depth