Knowledge Base

Get A Comma-Separated List of An Author's Roles

PHP code to help list out all of a WordPress user's roles on the frontend

Last updated on January 25, 2023
I recently built a job board-style site where I needed to list out all of a WordPress user (author's) roles on the frontend. You can use the snippet below, along with get_user_role to help list them out in your template.
<?php function get_user_role( $user_id ) { global $wp_roles; $roles = array(); $user = new WP_User( $user_id ); $current_author = get_the_author_meta('ID'); if ( !empty( $user->roles ) && is_array( $user->roles ) ) { foreach ( $user->roles as $role ) $roles[] .= translate_user_role( $role ); } return implode(', ',$roles); } ?>
To get this list using Oxygen Builder anywhere that accepts dynamic values, simply click "Insert Data," scroll to Custom PHP, input get_user_role, then click, "Insert."