Change WordPress user roles and capabilities Forums How to or FAQ Show user role after name in frontend Reply To: Show user role after name in frontend

#4922
Vladimir
Keymaster

You need to change the template which outputs user’s name, add user’s roles there using a function like below. It returns the string of comma separated user roles by user ID:


function get_user_roles($user_id) {

    $wp_roles = wp_roles();
    $user = get_user_by('id', $user_id);
    
    $roles = array();
    foreach ($user->roles as $role_id) {
        $roles[] = $wp_roles->role_names[$role_id];
    }
    
    $roles_str = implode(', ', $roles);

    return $roles_str;
}