Can anyone explain why I cannot see the role from the code below:

$userID = get_current_user_id();
$user = get_userdata(userID);
<?php echo $user->roles; ?>

but I can see all the other user data such as

<?php echo $user->ID; ?>
<?php echo $user->user_login; ?>

I'm following this documentation.


You can't see any roles printed, because the ->roles field is an Array, so you can't print it using echo. User print_r instead.

You also have an error in this line:

$user = get_userdata(userID);

There is no such thing like userID - it should be $userID.


Please change

$user = get_userdata(userID);


$user = get_userdata(YOUR_USER_ID_VARIABLE);

And check using print_r($user->roles);

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.