Languages:
English •
日本語
(Add your language)
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, $args ); ?>
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 Person."
- Default:
- alt
- (string) (optional) Alternate text for the avatar.
- Default: ``
- args
- (array) (optional) Arguments to return instead of the default arguments.
- Default: null
$args
Note: If you change the $defaults in your get_avatar using $new_defaults, you must declare the $new_defaults BEFORE you call get_avatar, otherwise, they won't take effect.
Default values:
- size
- (int) (optional) Height and width of the avatar image file in pixels.
- Default: 96'
- height
- (int) (optional) Display height of the avatar in pixels.
- Default: Defaults to $size.
- width
- (int) (optional) Display width of the avatar in pixels.
- Default: Defaults to $size.
- default
- (string) (optional) URL for the default image or a default type. Accepts '404' (return a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster), 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm', or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or 'gravatar_default' (the Gravatar logo).
- Default: Default is the value of the 'avatar_default' option, with a fallback of 'mystery'.
- force_default
- (bool) (optional) Whether to always show the default image, never the Gravatar.
- Default: false.
- rating
- (string) (optional) What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are judged in that order.
- Default: Default is the value of the 'avatar_rating' option.
- scheme
- (string) (optional) URL scheme to use. See set_url_scheme() for accepted values.
- Default:
- class
- (array|string) (optional) Array or string of additional classes to add to the <img> element.
- Default: null
- force_display
- (bool) (optional) Whether to always show the avatar - ignores the show_avatars option.
- Default: false
- extra_attr
- (string) (optional) HTML attributes to insert in the IMG element. Is not sanitized.
- Default: empty.
Return Values
- string|false
- An img element for the user's avatar or false on failure. 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 ); ?>
For a specific user id:
<?php
$user_id = 2;
echo get_avatar( $user_id, 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
- Since: 4.2.0 Optional `$args` parameter added.
- Since: 2.5.0
Source File
get_avatar() is located in wp-includes/pluggable.php
.
Related