Show user role name instead of user role id in wordpress


Staff member
I've made a change to the attendee list of Event Manager to show the user role of attendees.

Got it to work using this code:

    $aanmelding = $EM_Booking->get_person()->ID;
    $user = new WP_User( $aanmelding ); 

    if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
    foreach ( $user->roles as $role )

        echo '<li>'. $EM_Booking->get_person()->get_name() .' - ' . $role  .' - Aantal personen : '  . $EM_Booking->get_spaces() .  '</li>';

It works, but I want to display the role name instead of the role id. So instead of having subscriber, I'd like it to use the name I've given it, which is Guest.

I've found this stackoverflow code, but I could not get it adapted to my setup, since I'm not using the wp user role global.

I did find a topic on that uses if and elseif's to change the words from the role ID, but that removed the rest of the info and only displayed the changed role ID word.

 if ($user_role == 'administrator') {
 echo 'Administrator';
 } elseif ($user_role == 'editor') {
 echo 'Editor';
 } elseif ($user_role == 'author') {
 echo 'Author';
 } elseif ($user_role == 'contributor') {
 echo 'Contributor';
 } elseif ($user_role == 'subscriber') {
 echo 'Subscriber';
 } else {
 echo '<strong>' . $user_role . '</strong>';

When I tried pasting my booking->get_person bit in these elseif's, it did not work anymore.

Anyone able to help me out? I'm happy with myself I got this far with google foo and some experimenting. I'm not adept enough to take it the last bit!