
Definition
In WordPress, get_avatar() returns the avatar image of a user, an email address, or a comment. It returns an img tag as a string.
Avatars come from Gravatar by default. Because the function returns the HTML, you must use echo to show it.
Syntax
Pass a user ID, an email, or a comment, and a size in pixels.
echo get_avatar( $comment, 48 );
echo get_avatar( '[email protected]', 64 );
echo get_avatar( get_the_author_meta( 'ID' ), 96 );
| Call | What it does |
|---|
get_avatar( $comment, 48 ) | Returns the avatar of a comment author, 48 pixels wide. |
get_avatar( '[email protected]', 64 ) | Returns the avatar for an email address. |
get_avatar( 5, 96 ) | Returns the avatar of the user with ID 5. |
Use get_avatar_url() if you only need the image address.
Full declaration
get_avatar( mixed $id_or_email, int $size = 96, string $default_value = '', string $alt = '', array $args = null ): string|false
Parameters
| Parameter | Description |
|---|
$id_or_email | Required. A user ID, an email address, a WP_User, a WP_Post, or a WP_Comment object. |
$size | Optional. Width and height in pixels. Default 96. |
$default_value | Optional. The default avatar when none exists, such as mystery or identicon. |
$alt | Optional. The alt text of the image. Default is empty. |
$args | Optional. An array with keys such as class, height, width, rating, loading, and force_default. |
Return Value
| Return Value | Description |
|---|
| string|false | The img tag as a string. It returns false when avatars are turned off in Settings, Discussion. |
WordPress Examples
Three short theme examples. Each one has a sample output.
Example 1 — Comment avatar
Show the avatar of a commenter.
<div class="avatar-wrap">
<?php echo get_avatar( $comment, 48 ); ?>
</div>
<div class="avatar-wrap">
<img alt='' src='https://secure.gravatar.com/avatar/abc123?s=48&d=mm&r=g' class='avatar avatar-48 photo' height='48' width='48' loading='lazy' decoding='async'/>
</div>
How It Works
$comment is passed to your callback.- The size
48 sets the width and the height. echo prints the returned HTML.
Example 2 — From an email
Show the avatar for an email address with alt text.
<img alt='Jane Doe' src='https://secure.gravatar.com/avatar/abc123?s=64&d=mm&r=g' class='avatar avatar-64 photo' height='64' width='64' loading='lazy' decoding='async'/>
How It Works
- The first argument is an email address.
- The fourth argument sets the alt text.
- WordPress finds the Gravatar for that email.
Example 3 — Add a class
Add your own CSS class with the args array.
<?php echo get_avatar( $comment, 48, '', '', array( 'class' => 'round' ) ); ?>
<img alt='' src='https://secure.gravatar.com/avatar/abc123?s=48&d=mm&r=g' class='round avatar avatar-48 photo' height='48' width='48' loading='lazy' decoding='async'/>
How It Works
- The fifth argument is an array of options.
- The key
class adds your class name. - Style
.round in your CSS.
Frequently Asked Questions
It returns the img tag of the avatar for a user, an email, or a comment.
get_avatar() returns the HTML. You must echo it.
By default from Gravatar, based on the email address.
The function returns false. Turn them on in Settings, Discussion.
Use get_avatar_url().
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful