Sometimes you just want to simply recognize your editors, contributors … and maybe even your subscribers. I’d like to show you how to recognize the user roles, or capabilities of the various post authors. This may come in handy for those sites with multiple editors and/or contributors (and even administrators).

Let’s consider the singular view of posts (and pages) and what you can do when you recognize the specific post (or page) author’s role. You could simply add some text to display the post was written by an editor or a contributor (depending on what the user can do at the time this detail is displayed).

Let’s start by looking at the user_can function, here is its codex page:

The user_can function has two parameters: the $user and the $capability. Let’s look at recognizing these $users in singular template views. We just need to find the user (object) and compare it to the common capabilities …

global $post; // optional
if ( user_can( $post->post_author, 'administrator' ) ) {
	echo 'Administrator';
} elseif ( user_can( $post->post_author, 'editor' ) ) {
	echo 'Editor';
} elseif ( user_can( $post->post_author, 'author' ) ) {
	echo 'Author';
} elseif ( user_can( $post->post_author, 'contributor' ) ) {
	echo 'Contributor';
} elseif ( user_can( $post->post_author, 'subscriber' ) ) {
	echo 'Subscriber';
} else {
	echo '<strong>Guest</strong>';

Here is a link to this code in a gist.

Using the $post global variable containing all post data we can get the post_author detail which satisfies the $user parameter, and the strings representing the common roles/capabilities of users is simply tested against. As a simply exercise, once there is a match the role is displayed to the screen.

I wouldn’t necessarily recommend displaying the “Administrator” posts (the above is meant as an example only), but how could you put this little snippet to use? Feel free to share your ideas in the comments below.