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 = BricksQuery::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