WordPress get_avatar() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Template tag

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.

WordPress
echo get_avatar( $comment, 48 );
echo get_avatar( '[email protected]', 64 );
echo get_avatar( get_the_author_meta( 'ID' ), 96 );
CallWhat 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
WordPress
get_avatar( mixed $id_or_email, int $size = 96, string $default_value = '', string $alt = '', array $args = null ): string|false

Parameters

ParameterDescription
$id_or_emailRequired. A user ID, an email address, a WP_User, a WP_Post, or a WP_Comment object.
$sizeOptional. Width and height in pixels. Default 96.
$default_valueOptional. The default avatar when none exists, such as mystery or identicon.
$altOptional. The alt text of the image. Default is empty.
$argsOptional. An array with keys such as class, height, width, rating, loading, and force_default.

Return Value

Return ValueDescription
string|falseThe 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.

WordPress
<div class="avatar-wrap">
  <?php echo get_avatar( $comment, 48 ); ?>
</div>

How It Works

  1. $comment is passed to your callback.
  2. The size 48 sets the width and the height.
  3. echo prints the returned HTML.

Example 2 — From an email

Show the avatar for an email address with alt text.

WordPress
<?php echo get_avatar( '[email protected]', 64, '', 'Jane Doe' ); ?>

How It Works

  1. The first argument is an email address.
  2. The fourth argument sets the alt text.
  3. WordPress finds the Gravatar for that email.

Example 3 — Add a class

Add your own CSS class with the args array.

WordPress
<?php echo get_avatar( $comment, 48, '', '', array( 'class' => 'round' ) ); ?>

How It Works

  1. The fifth argument is an array of options.
  2. The key class adds your class name.
  3. 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().

Next: next_comments_link()

Print the link to the next page of comments.

next_comments_link() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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