Using WordPress ‘get_avatar_url()’ PHP function

The get_avatar_url() WordPress PHP function retrieves the avatar URL for a given user or object.


get_avatar_url($id_or_email, $args);


  • $id_or_email (mixed) – The Gravatar 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) – Optional. Arguments to use instead of the default arguments.
    • size (int) – Height and width of the avatar in pixels. Default 96.
    • default (string) – URL for the default image or a default type. See the description for accepted values. Default is the value of the ‘avatar_default’ option, with a fallback of ‘mystery’.
    • 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.

More information

See WordPress Developer Resources: get_avatar_url


Display the avatar of the current user

$user = wp_get_current_user();
if ($user) :
    echo '<img src="' . esc_url(get_avatar_url($user->ID)) . '" />';

Retrieve the avatar with a custom size

echo get_avatar_url($user->ID, ['size' => 51]);

Build a responsive user image

echo '<picture>
    <source srcset="' . get_avatar_url($user->ID, ['size' => 51]) . '" media="(min-width: 992px)" />
    <img src="' . get_avatar_url($user->ID, ['size' => 40]) . '" />

Create a dynamic favicon from the user’s avatar

echo '<link rel="shortcut icon" href="' . get_avatar_url($user->ID, ['size' => 16]) . '" />';

Get the avatar URL for a user by their login

$permalink = $_SERVER['REQUEST_URI'];
$username = explode("/", $permalink)[count(explode("/", $permalink)) - 1];
$user = get_user_by('login', $username);
echo get_avatar_url($user->ID);