Get the Post Author’s Avatar, Nickname, and Other Profile Data in WordPress

On multi-user WordPress sites, displaying a user’s avatar, nickname, and related profile information in the right places can clearly improve user participation and the overall experience. WordPress comment avatars are a good example of this. So how do we get an author’s avatar, nickname, and other profile data inside a post? It is actually very simple.

The two functions needed to get an author avatar

The function for getting a user avatar is get_avatar(). It uses the user’s ID or email address to retrieve the user’s universal avatar from Gravatar.

To get the user ID or email address, we can use get_the_author_meta(). This function accepts two parameters: the user field we want to retrieve and the user ID. If you are already inside the post loop, the second parameter is not required, because WordPress will use the current post author’s user ID by default.

Code examples for getting the author’s avatar

By combining the two functions above, we can retrieve the author’s avatar very easily.

<?php echo get_avatar( get_the_author_meta( 'user_email' ) ); ?>

You can also retrieve it by user ID. The two methods are equivalent.

<?php echo get_avatar( get_the_author_meta( 'ID' ) ); ?>

Get the post author’s name and author archive link

<a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>">
    <?php the_author(); ?>
</a>

Get additional information about the author

Besides the user avatar, we may also need other author details. Retrieving them is simple: just pass the appropriate field name into get_the_author_meta(). Available parameters include:

  • user_login
  • user_pass
  • user_nicename
  • user_email
  • user_url
  • user_registered
  • user_activation_key
  • user_status
  • roles
  • display_name
  • nickname
  • first_name
  • last_name
  • description (author bio)
  • jabber
  • aim
  • yim
  • googleplus
  • twitter
  • user_level
  • user_firstname
  • user_lastname
  • rich_editing
  • comment_shortcuts
  • admin_color
  • plugins_per_page
  • plugins_last_view
  • ID

In addition to these built-in user fields, plugins and themes may add extra user data as custom fields. For user information added by themes or plugins, you can retrieve it with the get_user_meta() function.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *