How do I get the avatar URL instead of an HTML IMG tag when using get_avatar?

Good news for WordPress versions 4.2+

Since version 4.2 the handy get_avatar_url() function, introduced as a feature request in ticket #21195 few years ago, now ships with the core:

/**
 * Retrieve the avatar URL.
 *
 * @since 4.2.0
 *
 * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash,
 *                           user email, WP_User object, WP_Post object, or comment object.
 * @param array $args {
 *     Optional. Arguments to return instead of the default arguments.
 *
 *     @type int    $size           Height and width of the avatar in pixels. Default 96.
 *     @type string $default        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 'mysterman' (The Oyster Man), 'blank' (transparent GIF), or
 *                                  'gravatar_default' (the Gravatar logo). Default is the value of the
 *                                  'avatar_default' option, with a fallback of 'mystery'.
 *     @type bool   $force_default  Whether to always show the default image, never the Gravatar. Default false.
 *     @type string $rating         What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
 *                                  judged in that order. Default is the value of the 'avatar_rating' option.
 *     @type string $scheme         URL scheme to use. See set_url_scheme() for accepted values.
 *                                  Default null.
 *     @type array  $processed_args When the function returns, the value will be the processed/sanitized $args
 *                                  plus a "found_avatar" guess. Pass as a reference. Default null.
 * }
 * @return false|string The URL of the avatar we found, or false if we couldn't find an avatar.
 */
function get_avatar_url( $id_or_email, $args = null ) {
    $args = get_avatar_data( $id_or_email, $args );
    return $args['url'];
}

where get_avatar_data() is also a new helper function.

It contains this code part:

... CUT ...

/**
 * Filter whether to retrieve the avatar URL early.
 *
 * Passing a non-null value in the 'url' member of the return array will
 * effectively short circuit get_avatar_data(), passing the value through
 * the {@see 'get_avatar_data'} filter and returning early.
 *
 * @since 4.2.0
 *
 * @param array             $args          Arguments passed to get_avatar_data(), after processing.
 * @param int|object|string $id_or_email   A user ID, email address, or comment object.
 */
$args = apply_filters( 'pre_get_avatar_data', $args, $id_or_email );
if ( isset( $args['url'] ) && ! is_null( $args['url'] ) ) {
    /** This filter is documented in wp-includes/link-template.php */
    return apply_filters( 'get_avatar_data', $args, $id_or_email );
}

... CUT ...

where we can see that when the url parameter is set, the available filters are pre_get_avatar_data and get_avatar_data.

After upgrading to 4.2 recently, I had a problem with a theme that defined it’s own version of get_avatar_url(), without any function name prefixing or a function_exists() check. So this is an example of why that’s important 😉

Leave a Comment