get_avatar_url()
Retrieves the avatar URL.
No Hooks.
Return
String|false
. The URL of the avatar on success, false on failure.
Usage
get_avatar_url( $id_or_email, $args );
- $id_or_email(mixed) (required)
- The avatar to retrieve a URL for. Accepts a user ID, Gravatar MD5 hash, user email, WP_User object, WP_Post object, or WP_Comment object.
- $args(array)
Arguments to use instead of the default arguments.
Default: null
-
size(int)
Height and width of the avatar in pixels.
Default: 96 -
default(string)
URL for the default image or a default type. Accepts:- '404' (return a 404 instead of a default image)
- 'retro' (a 8-bit arcade-style pixelated face)
- 'robohash' (a robot)
- 'monsterid' (a monster)
- 'wavatar' (a cartoon face)
- 'identicon' (the "quilt", a geometric pattern)
- 'mystery', 'mm', or 'mysteryman' (The Oyster Man)
- 'blank' (transparent GIF)
- 'gravatar_default' (the Gravatar logo) Default is the value of the 'avatar_default' option, with a fallback of 'mystery'.
-
force_default(true|false)
Whether to always show the default image, never the Gravatar.
Default: false -
rating(string)
What rating to display avatars up to. Accepts:- 'G' (suitable for all audiences)
- 'PG' (possibly offensive, usually for audiences 13 and above)
- 'R' (intended for adult audiences above 17)
- 'X' (even more mature than above) Default is the value of the 'avatar_rating' option.
-
scheme(string)
URL scheme to use. See set_url_scheme() for accepted values.
Default: null - processed_args(array)
When the function returns, the value will be the processed/sanitized $args plus a "found_avatar" guess. Pass as a reference.
Default: null
-
Examples
#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
#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">
#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().
#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() get avatar url code WP 6.6.2
function get_avatar_url( $id_or_email, $args = null ) { $args = get_avatar_data( $id_or_email, $args ); return $args['url']; }