How To Get The URL To a Gravatar Image

You’ve probably seen the following encoded URL before like this:

//0.gravatar.com/avatar/d5279c8b66d25549a0ec3c8dd46a3d1a?s=120

This URL enables you to link to a Gravatar image using the encoded email address of the Gravatar account. However, you need to MD5 hash the email address for the Gravatar account to get the hash.

All Gravatar URL’s are based on the use of the hashed value of an email address.

To create a hash, follow these 3 simple steps:

  1. Trim leading and trailing whitespace from an email address
  2. Force all characters to lower-case
  3. md5 hash the final string

You can then use the hash to request the image using code like this:

add_action( 'loop_start', 'print_gravatar_image' );
function print_gravatar_image() {

$gravatar = '//0.gravatar.com/avatar/d5279c8b66d25549a0ec3c8dd46a3d1a?s=180';

printf( '<img src="%s" />', esc_url( $gravatar ) );
}

Or like this in a template file for default themes like Twenty Sixteen.

<?php
$gravatar = '//0.gravatar.com/avatar/d5279c8b66d25549a0ec3c8dd46a3d1a?s=180';
printf( '<img src="%s" />', esc_url( $gravatar ) );
}
?>

Related Tutorials

Was This Tutorial Helpful?

Free

$0

Access only to all free tutorials per month.



Monthly

$75

Access to 10 premium tutorials per month.


Tutorial Request


Includes code guarantee and coding support.

Yearly

$500

Access to 15 premium tutorials per month.


Monthly Tutorial Request


Includes code guarantee and priority coding support.