3

I'm working on a site and I need to get the type of user the author is so I can mark it.

The idea is that if an Admin level user post something it has a [ADMIN] tag or something next to their name. I know how to compare things within php but how do I check to see what user type an author is if possible? I can't seem to find any good documentation on the matter.

2 Answers 2

1

One way would be to use the get_the_author_meta function and pass it user_level

More info on the function: http://codex.wordpress.org/Function_Reference/get_the_author_meta

Then, see this chart to convert user levels to roles (unless you have created custom roles): http://codex.wordpress.org/Roles_and_Capabilities#User_Level_to_Role_Conversion

Example code

$level = get_the_author_meta('user_level');

if($level >= 8) { // Is admin
    // do something
} else if ($level > 2 && $level < 8) { // is editor
    // do something else
}
0

in the loop:

the_author_meta('user_level'); 

will get you the capability number of the author. which is close, but not quite what you want.... though you could translate that into the default WP roles.

to get the roles, you could do the following (which will spit out ALL the roles a user has) :

$user = new WP_User( get_the_author_meta('ID'));

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

http://wordpress.org/support/topic/get-a-users-role-by-user-id?replies=20#post-1363118

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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