The get_avatar_data() WordPress PHP function retrieves default data about the avatar.
Usage
get_avatar_data( $id_or_email, $args )
Custom example:
$avatar_data = get_avatar_data( '[email protected]', array( 'size' => 150 ) ); echo $avatar_data['url'];
Parameters
-
$id_or_email
(mixed) (Required): The avatar to retrieve. Accepts a user ID, Gravatar MD5 hash, user email, WP_User object, WP_Post object, or WP_Comment object. -
$args
(array) (Optional): Arguments to use instead of the default arguments. See below for available options.size
(int): Height and width of the avatar image file in pixels. Default 96.height
(int): Display height of the avatar in pixels. Defaults to$size
.width
(int): Display width of the avatar in pixels. Defaults to$size
.default
(string): URL for the default image or a default type. See below for accepted values.force_default
(bool): Whether to always show the default image, never the Gravatar. Default false.rating
(string): What rating to display avatars up to. Accepts ‘G’, ‘PG’, ‘R’, ‘X’. Default is the value of the ‘avatar_rating’ option.scheme
(string): URL scheme to use. See set_url_scheme() for accepted values.processed_args
(array): When the function returns, the value will be the processed/sanitized$args
plus a"found_avatar"
guess. Pass as a reference.extra_attr
(string): HTML attributes to insert in the IMG element. Is not sanitized. Default empty.
More information
See WordPress Developer Resources: get_avatar_data()
Examples
Display avatar by email address
This example will display an avatar with a size of 150 pixels using the email address.
$avatar_data = get_avatar_data( '[email protected]', array( 'size' => 150 ) ); echo '<img src="' . $avatar_data['url'] . '" width="' . $avatar_data['width'] . '" height="' . $avatar_data['height'] . '">';
Display avatar by user ID
This example will display an avatar with a size of 200 pixels using the user ID.
$user_id = 1; $avatar_data = get_avatar_data( $user_id, array( 'size' => 200 ) ); echo '<img src="' . $avatar_data['url'] . '" width="' . $avatar_data['width'] . '" height="' . $avatar_data['height'] . '">';
Display avatar with a custom default image
This example will display an avatar with a custom default image if the user does not have a Gravatar.
$avatar_data = get_avatar_data( '[email protected]', array( 'default' => 'https://example.com/default-avatar.png' ) ); echo '<img src="' . $avatar_data['url'] . '" width="' . $avatar_data['width'] . '" height="' . $avatar_data['height'] . '">';