Comma-separated user role names in Bricks

Looking to display comma-separated names of user roles for the current user (in the loop) in a Bricks site?

Here’s how.

Step 1

Add the following in child theme‘s functions.php or a code snippets plugin:

// Function to output comma-separated names of user roles for the current user in the loop
function bl_get_user_roles_names():string {
	// get the user roles for the current user in the loop
	$roles = Bricks\Query::get_loop_object()->roles;

	$roles_names = [];
	
	foreach ( $roles as $role ) {
		$roles_names[] = wp_roles()->get_names()[ $role ];
	}
	
	$roles_names_string = implode( ', ', $roles_names );
	
	return $roles_names_string;
}

Step 2

Set up a query loop of the type “Users”.

Add a Basic Text element and set its text to:

{echo:bl_get_user_roles_names}

Note: WordPress does not have a built-in interface/option for assigning multiple roles to a user. But it is possible using a plugin or custom code.

Reference

https://wordpress.stackexchange.com/a/360384

Instant access to 390+ Bricks code tutorials with BricksLabs Pro

5 comments

  • Christophe Sansonetti

    hum really sorry but no, it show the 1st role of the differents users roles, but not the one i selected in admin ๐Ÿ™

  • Christophe Sansonetti

    solved it, sorry !

  • Christophe Sansonetti

    Hi Sridhar, that works fine but it show me all the roles, not the current user one ๐Ÿ™

Leave your comment