0

I'm looking to create a list of all the users on my wordpress site with their email and role.

$blogusers = get_users();
// Array of WP_User objects.
foreach ( $blogusers as $user ) {
    echo '<span>' . esc_html( $user->user_email ) . '</span>';
    echo '<span>' . esc_html( $user->roles ) . '</span>';
}

This function is sort of what I am looking for but the user role displays as an array. Any thoughts on an easier way to do this?

Thanks

1

That's the correct way to get what you need. Though I'm not sure why WordPress returns an array of roles since ( to my knowledge ) you can only have 1 role at a time. Roles being an array you can modify by just retrieving the role index 0:

$blogusers = get_users();
// Array of WP_User objects.
foreach ( $blogusers as $user ) {
    echo '<span>' . esc_html( $user->user_email ) . '</span>';
    echo '<span>' . esc_html( $user->roles[0] ) . '</span>';
}
  • "you can only have 1 role at a time"... you can have more than 1 role at a time :) – Pete Dec 18 '16 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.