get_avatar() returns the HTML image tag for a user’s avatar. Give it a user ID, email address, or a user, post or comment object, plus a size in pixels, and echo the result: echo get_avatar( get_current_user_id(), 96 ); If you only need the image address, use get_avatar_url() instead.
By default both functions pull from Gravatar, based on the user’s email address, and they work with plugins that replace Gravatar with locally uploaded avatars. Everything below is checked against the WordPress 7.1 source.
get_avatar()returns a complete<img>tag.get_avatar_url()returns just the image address.- Both accept a user ID, an email address, or a
WP_User,WP_PostorWP_Commentobject. A post gives you its author’s avatar. get_avatar()returnsfalsewhen Show Avatars is turned off in Settings > Discussion, unless you pass'force_display' => true.- WordPress adds a double-size version for sharp avatars on high-density screens, and since version 6.8 it looks up Gravatars with a SHA-256 hash of the email.
- Change the fallback image in Settings > Discussion, or use the
pre_get_avatar_datafilter to serve your own avatars instead of Gravatar.
get_avatar() syntax and parameters
get_avatar( $id_or_email, $size = 96, $default_value = '', $alt = '', $args = null );
| Parameter | What it does |
|---|---|
$id_or_email | Whose avatar to show: a user ID, email address, Gravatar hash, or a WP_User, WP_Post or WP_Comment object. |
$size | Width and height in pixels. Default 96. |
$default_value | The image to use when there is no Gravatar. Leave it empty to use the default chosen in Settings > Discussion. |
$alt | Alt text for the image. Empty by default. |
$args | An array of extra options: class, force_display, force_default, rating, height, width, scheme, loading, decoding, fetchpriority and extra_attr. |
The image tag it returns carries the classes avatar, avatar-96 (with your size) and photo, plus avatar-default when the fallback image is used, so you can style avatars with CSS. See the get_avatar() reference for every option.
Show the current user’s avatar
Pass get_current_user_id() and check that someone is logged in first, so visitors do not get the default image:
<?php
if ( is_user_logged_in() ) {
echo get_avatar( get_current_user_id(), 48 );
}
This is the usual way to put an avatar in a custom header or account menu. Our guide to getting a user ID in WordPress covers the other ways to find the ID you need.
Show the post author’s avatar
Inside the loop, get the author’s ID with get_the_author_meta( 'ID' ). Outside it, you can pass the post object itself, and WordPress uses the post’s author:
<?php
// Inside the loop.
echo get_avatar( get_the_author_meta( 'ID' ), 64 );
// Outside the loop: pass the post, and WordPress finds its author.
echo get_avatar( get_post( 123 ), 64 );
Get just the avatar URL with get_avatar_url()
get_avatar_url() accepts the same first argument and returns the image address, or false if it cannot build one. Use it when you want to write your own markup or use the avatar as a background image:
<?php
$user_id = get_current_user_id();
$url = get_avatar_url( $user_id, array( 'size' => 128 ) );
if ( $url ) {
echo '<img src="' . esc_url( $url ) . '" alt="" width="128" height="128">';
}
Add a class, alt text or loading attribute
The fifth argument takes an array of options. This version adds a CSS class and alt text, loads the image straight away instead of lazily, and shows the avatar even if avatars are switched off site-wide:
<?php
echo get_avatar(
get_current_user_id(),
80,
'',
'Your profile photo',
array(
'class' => 'profile-avatar',
'loading' => 'eager',
'force_display' => true,
)
);
Use 'loading' => 'eager' only for an avatar that appears near the top of the page. For avatars further down, such as in comments, leave it out and let WordPress decide.
Change the default avatar
The image shown for people without a Gravatar is set in Settings > Discussion, under Default Avatar. WordPress 7.1 offers ten: Mystery Person, Blank, Gravatar Logo, Identicon, Wavatar, MonsterID, Retro, RoboHash, Initials and Color. Identicon, Wavatar, MonsterID, Retro, RoboHash and Color are generated from each person’s email address, and Initials uses the first letters of their display name, so everyone gets a distinct image.
To add your own image to that list, use the avatar_defaults filter in your functions.php file, then select it in Settings > Discussion. The image has to be at a public URL, because Gravatar fetches it:
<?php
add_filter( 'avatar_defaults', function ( $defaults ) {
$defaults['https://example.com/images/default-avatar.png'] = 'Company logo';
return $defaults;
} );
Use your own avatars instead of Gravatar
If you store avatars yourself, the pre_get_avatar_data filter lets you hand WordPress an image URL before it contacts Gravatar. When the filter sets $args['url'], WordPress uses it and skips the Gravatar lookup. This example reads a URL saved in user meta:
<?php
add_filter( 'pre_get_avatar_data', function ( $args, $id_or_email ) {
$user_id = 0;
if ( is_numeric( $id_or_email ) ) {
$user_id = (int) $id_or_email;
} elseif ( $id_or_email instanceof WP_User ) {
$user_id = $id_or_email->ID;
} elseif ( $id_or_email instanceof WP_Post ) {
$user_id = (int) $id_or_email->post_author;
} elseif ( $id_or_email instanceof WP_Comment ) {
$user_id = (int) $id_or_email->user_id;
}
$custom = $user_id ? get_user_meta( $user_id, 'custom_avatar_url', true ) : '';
if ( $custom ) {
$args['url'] = $custom;
}
return $args;
}, 10, 2 );
Because get_avatar(), get_avatar_url() and comment avatars all go through this filter, one snippet changes the avatar everywhere on your site. Local avatar plugins work through these same filters.
Why get_avatar() returns false or the wrong image
- It returns false: avatars are switched off. Turn on Show Avatars under Settings > Discussion, or pass
'force_display' => true. - It always shows the default image: that email address has no Gravatar, or you passed
'force_default' => true. Check the ID you are passing too, since an invalid ID also falls back to the default. - The image is blurry: the size is too small for the space. WordPress already adds a double-size version for high-density screens, so pass the size you actually display.
- Some avatars are missing: the Maximum Rating setting in Settings > Discussion hides Gravatars rated above it.
Under the hood, WordPress has requested Gravatars over HTTPS since version 6.7 and has used a SHA-256 hash of the email address since version 6.8, rather than the older MD5 hash.
Frequently asked questions
What does get_avatar() return?
An HTML img tag for the avatar, as a string, or false when avatars are switched off in Settings, Discussion and you did not pass force_display. Echo it to print the image.
How do I get an avatar URL by user ID?
Call get_avatar_url( $user_id ). It returns the image URL as a string, or false if WordPress cannot build one. Pass a second argument such as array( ‘size’ => 128 ) to set the size.
How do I get the avatar of a post author in WordPress?
Inside the loop, use get_avatar( get_the_author_meta( ‘ID’ ), 64 ). Outside the loop, pass the post object, for example get_avatar( get_post( 123 ), 64 ), and WordPress uses the post’s author.
How do I change the default avatar in WordPress?
Go to Settings, Discussion and pick one under Default Avatar. To offer your own image there, add it with the avatar_defaults filter, using a public image URL.
Can I use get_avatar() without Gravatar?
Yes. Use the pre_get_avatar_data filter to return your own image URL, or install a local avatar plugin, which uses the same filter. You can also switch avatars off entirely under Settings, Discussion.
For more functions like this, see our collection of useful WordPress code snippets, or learn how to manage WordPress user roles.



28 Responses
nice post
Are you looking for a digital marketing strategist…..Hey, I am NIHUL T Google Certified Freelance Digital Marketing Strategist in Calicut, Kerala, India. I can help you level up your business with online marketing strategies.
As a Digital Marketing Strategist in Calicut, I can solve your Marketing problems through online channels like SEO, SEM, SMM, Content Marketing, Web Development, Email Marketing . It's my passion so I can give it all my effort with your Business.
Are you trying to find a digital marketing strategist? Hello, my name is Dilshan KP, and I work as a freelance digital marketing strategist in Calicut, Kerala, India. I am a Google Certified Professional. Using internet marketing techniques, I can help you take your company to the next level.
Hi Dilshan,
Thanks for reaching out! It's great to know about your expertise as a Google Certified Digital Marketing Strategist. I'd love to know more about the services you offer and how you can specifically help businesses grow online. Could you share some details about your past projects or areas you specialize in (like SEO, paid ads, social media,
Your content is always appreciated. Thanks for sharing!
Thank you for sharing this! The training looks very promising. Could you please share more details about the course structure, duration, and certifications offered?
Fantastic breakdown! The simplicity and flexibility of get_avatar() and get_avatar_url() make it so easy to add that personal touch to a WordPress site. Thanks for sharing these practical tips!
salon for ladies in sharjah
best digital marketing strategist in kannur
are you looking for digital marketing strategist
digital marketing strategist
enthakke und vishesham😁
Are you trying to find a strategist for digital marketing?I’m Isham Nadiq, a freelance digital marketing strategist based in Calicut, Kerala, India. I am Google Certified. I can use internet marketing techniques to assist you take your company to the next level.
I can help with your marketing issues using online channels including SEO, SEM, SMM, content marketing, web development, and email marketing as a digital marketing strategist in Calicut. Since it’s my passion, I can put my all into your business.
I'm abhinav, a passionate digital marketer with a knack for crafting effective online strategies. With experience in SEO, content marketing, social media, and data analytics, I help brands connect with their audience in meaningful ways. My goal is to drive engagement, increase conversions, and leverage the latest digital trends to achieve measurable results. I love staying up-to-date with industry changes and finding innovative solutions to marketing challenges. Let's work together to elevate your brand's online presence!
Hi Abhinav,
Great to hear about your passion and expertise in digital marketing! Your skills in SEO, content marketing, social media, and analytics sound really impressive. I'd love to know more about the kind of projects you've worked on and how you usually approach building strategies for brands. Let's definitely explore how we can collaborate to create impactful results.
Informative blog. Thank you for sharing.
Discover the best beauty salon in Abu Dhabi at Snow White Beauty Salon. Our skilled professionals offer luxurious services, including expert hair care, revitalizing facials, relaxing Moroccan baths, and meticulous nail care. Using premium products in a serene atmosphere, we ensure personalized treatments that leave you feeling refreshed and beautiful. Visit Snow White Beauty Salon today and experience top-tier beauty and wellness.
Are you looking for a digital marketing strategist? Hey, I am **Siraj**, a Google Certified Freelance Digital Marketing Strategist in Calicut, Kerala, India. I can help you level up your business with online marketing strategies.
As a Digital Marketing Strategist in Calicut, I can solve your marketing problems through online channels like SEO, SEM, SMM, Content Marketing, Web Development, and Email Marketing. It’s my passion, so I can give it all my effort with your business.
Thanks for reaching out! It's great to know about your expertise as a Google Certified Digital Marketing Strategist. I'd love to know more about the services you offer and how you can specifically help businesses grow online. Could you share some details about your past projects or areas you specialize in (like SEO, paid ads, social media, etc.)?
great!
good
hello
nice
great
great
Thank You
Best Digital Marketing Freelancer In kannur
https://nafia.in/
Thankyou for sharing. Informative.
Sea Drive Shipping LLC, a leading Freight Forwarder in Dubai, offers sea, land and air freight services, including Break Bulk, Full Container Load, Less Than Container Load and RoRo. With our global network, we ensure timely and secure delivery of your shipments.
Nice
Great advice, thanks for sharing