Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a pretty specific requirement to show different text in a field label on the user profile page based on the current user's role. I can't seem to figure out how to check whether the current use is an "author".

I am looking for a function like:

is_user_in_role($user, "author");

I imagine this is pretty simple, but I have searched for too long without an answer so I thought I would post it here.

share|improve this question
add comment

2 Answers 2

up vote 10 down vote accepted

If you only need this for current user current_user_can() accepts both roles and capabilities.

share|improve this answer
    
Wow. Thank you. That is exactly what I need. I saw that function elsewhere, but failed to realize that you could pass in a role name. You Rock! –  jessegavin Dec 8 '10 at 16:08
2  
I know this post is answered a long time ago but if someone happens to get here... look at the documentation once more for current_user_can() -> "Do not pass a role name to current_user_can(), as this is not guaranteed to work correctly (see #22624). Instead, you may wish to try the check user role function put together by AppThemes." (codex.wordpress.org/Function_Reference/current_user_can) –  bestprogrammerintheworld Jan 28 at 7:04
add comment

I was looking for a way to get a user's role using the user's id. Here is what I came up with:

function wp_get_user_roles_by_user_id( $user_id ) {
    $user = get_userdata( $user_id );
    return empty( $user ) ? array() : $user->roles;
}
share|improve this answer
    
works fine for me to get the first role assigned to a user. –  Q Studio Oct 10 '12 at 19:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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