WordPress.org

Ready to get started?Download WordPress

Codex

Function Reference/get avatar

Contents

Description

Retrieve the avatar for a user who provided a user ID or email address. Most commonly used in the comments section.

This function is pluggable, however plugin authors wishing to change the gravatar output should use the get_avatar filter instead, for compatibility purposes.

This function will not return an avatar if "Show Avatars" is unchecked in Settings > Discussion.

Usage

 <?php echo get_avatar$id_or_email$size$default$alt ); ?> 

Parameters

id_or_email
(integer/string/object) (required) Author’s User ID (an integer or string), an E-mail Address (a string) or the comment object from the comment loop.
Note: with most comment templates you can use $comment here, in order to display the gravatar of the commenter. In other templates within The Loop (for WordPress 2.7 and lower), you can use get_the_author_id() (deprecated in WordPress 2.8). For WordPress 2.8 and up, please use get_the_author_meta( 'ID' ).
Default: None
size
(integer) (optional) Size of Gravatar to return (max is 512).
Default: 96
default
(string) (optional) url for an image, defaults to the "Mystery Man".
Default:
alt
(string) (optional) Alternate text for the avatar.
Default: false

Return Values

string 
An img element. The function does not output anything; you have to echo the return value.

Examples

For comments:

<?php echo get_avatar( $comment, 32 ); ?>

For a post's author, in The Loop:

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

For a random email address:

<?php echo get_avatar( 'email@example.com', 32 ); ?>

Notes

  • Uses the get_avatar filter to filter the returned string.
  • This function is pluggable and so can be overridden by plugins and themes. The default functionality cannot be relied upon in these cases.

Changelog

Source File

get_avatar() is located in wp-includes/pluggable.php.

Related

See also index of Function Reference and index of Template Tags.
This article is marked as in need of editing. You can help Codex by editing it.