? The Snippet:
$userRoles = new WP_Roles(); $roles_names = $userRoles->get_names(); // Loop through all WP User Roles foreach ($roles_names as $role_name) { echo $role_name . '<br />'; }
ℹ️ About the Snippet
If you ever want to list all user roles in WordPress. Then, you can do this with the above-mentioned small code snippet. It’ll retrieve all the available user roles which include the default and registered user roles.
✅ How to use the Snippet
Add the below code snippet to wherever you want to show all the available WordPress user roles, and this will show all of those.
$userRoles = new WP_Roles(); $roles_names = $userRoles->get_names(); // Loop through all WP User Roles foreach ($roles_names as $role_name) { echo $role_name . '<br />'; }