get_avatar_url()WP 4.2.0

Gets the avatar URL based on the provided email, ID, or user object. You can also pass a post or comment object.

This function does not check if there is actually an avatar; it simply collects the URL from the gravatar.com service. To check if there is an avatar for the email, you need to process the obtained URL separately. You should specify 404 in default, then get the URL and try to fetch the image from that URL using get_headers(). If the response returns 404, then there is no avatar for the email. See examples.

See also the function get_avatar().

Used By: get_avatar()
1 time — 0.00008 sec (very fast) | 50000 times — 1.67 sec (fast) | PHP 7.0.8, WP 4.5.3

No Hooks.

Returns

String|false. The avatar URL (returned in 99% of cases). false if something went wrong in the function and it failed to collect the URL.

Usage

get_avatar_url( $id_or_email, $args );
$id_or_email(mixed) (required)

The identifier used to determine the avatar. It can be:

  • User ID;
  • User email;
  • Gravatar md5 hash (md5 hash of the email);
  • WP_User object;
  • WP_Post object - will get the avatar of the post author;
  • WP_Comment object - will get the avatar of the commenter.
$args(array)

Parameters for the obtained avatar. By default:

  • size(number)
    Height and width of the avatar in pixels.
    Default: 96
  • default(string)
    URL for the default image - if the avatar could not be obtained. Instead of a URL, you can specify the following values:

    • 404 - will return 404 instead of the default image.
    • retro - pixel image 8bit: ,
    • monsterid - monster: ,
    • wavatar - cartoon face: ,
    • indenticon - pattern image: ,
    • mystery, 'mm' or 'mysterman' - unknown person: ,
    • blank - transparent GIF image,
    • gravatar_default - Gravatar logo: .

    Default: the value of the 'avatar_default' option or 'mystery'

  • force_default(boolean)
    Set to true if you always want to show the default image and not show avatars at all.
    Default: false

  • rating(string)
    The maximum allowed rating for the displayed avatar. It can be:

    • G — Suitable for any audience;
    • PG — May contain offensive elements, usually for audiences over 13 years old;
    • R — Intended for adult audiences over 17 years old;
    • X — For even more mature audiences;

    Default: the value of the 'avatar_rating' option

  • scheme(string)
    The URL scheme to be used. It can be: 'http', 'https', 'login', 'login_post', 'admin', or 'relative'. For more details, see the set_url_scheme() function.
    Default: null

  • processed_args(array)
    Additional options to pass. When filtering and returning data from the get_avatar_data() function, these parameters will be added to the returned array.
    Default: null

  • extra_attr(string)
    HTML attributes to add to the IMG element. Not sanitized.
    Default: ''

Default: null

Examples

1

#1 Get the URL of the current user's avatar

$url = get_avatar_url( wp_get_current_user(), array(
	'size' => 48,
	'default'=>'wavatar',
) );

// http://1.gravatar.com/avatar/155e695ab2251ee3c482c1e3e690683b?s=48&d=identicon&r=g
0

#2 Display the avatar picture of the author of the current article [auto-translate]

global $post;
$url = get_avatar_url( $post, "size=24&default=monsterid");
$img = '<img alt="" src="'. $url .'">';
echo $img;

// <img alt="" src="http://1.gravatar.com/avatar/155e695ab2251ee3c482c1e3e690683b?s=24&d=wavatar&r=g">
0

#3 Checking if email has an avatar

To check if email has an avatar, you need to process the URL of the gravatar separately. In the parameter default specify 404, then get the URL and try to get a picture from that URL using get_headers(). If the response returns 404, then the email avatar doesn't have one.

// check if email has an avatar
function has_gravatar( $email ){

	$gravatar_url = get_avatar_url( $email, 'default=404' );
	$headers = @ get_headers( $gravatar_url  );

	return ! preg_match('|404|', $headers[0] );
}

var_dump( has_gravatar('[email protected]') ); // true
var_dump( has_gravatar('[email protected]') ); // false

// the execution time is from 0.1 to 1 second. It is very long - this is an HTTP request.

Such checking of request will slow down loading of the site, but if you really need, you can cache the request somewhere, for example in set_transient().

-13

#4 Get the avatar's URL from the user's email

$url = get_avatar_url( '[email protected]', array(
	'size' => 48,
	'default'=>'identicon',
) );

// http://1.gravatar.com/avatar/4b508f04b661ba693130539bd29f5c0d?s=48&d=identicon&r=g

Changelog

Since 4.2.0 Introduced.

get_avatar_url() code WP 6.8.1

function get_avatar_url( $id_or_email, $args = null ) {
	$args = get_avatar_data( $id_or_email, $args );
	return $args['url'];
}