Avatars are an important part of WordPress, no matter if you’re developing a theme or just custom functionality in this post we’ll go over all the ways to get a user’s avatar in WordPress.
Typically in WordPress the avatar will display a Gravatar but all of the functions below should work with plugins that replace Gravatar’s functionality in WordPress.
How to Display Avatar for Currently Logged in User
Getting the current logged in user’s avatar in WordPress is easy with get_avatar(). This function allows you to input a user ID, avatar size and output an image tag with the correct avatar.
I’ve also included a code snippet using get_avatar_url() which allows you to just retrieve the user’s avatar URL to be used in any way you’d like.
You can use the code snippet above for any custom functions on your WordPress site. For example adding the logged in user avatar to the header in a custom theme.
In my example function above I am also specifying the size of the avatar as 96. You can replace “96” with the height/width of the avatar you’d like. For a higher-resolution you could do 512 or for a lower-resolution avatar use 32.
You can also get the avatar for any user ID easily using the get_avatar and get_avatar_url functions mentioned above. This makes it easy for displaying the current post’s author avatar.
Using the get_the_author_meta() function we can grab the current post’s author ID to be used in our function to display the current post author avatar.
Of course you’ll have to use this code snippet inside the loop. Most likely where you’re using it in your theme or function it’ll likely work correctly.
I hope this article was helpful for understanding how to use get_avatar in your WordPress development. If you have any questions about WordPress development or WordPress code snippets let me know in the comments below.