Show Gravatar Image From Email

  • follow us in feedly
Published April 5, 2013 by Brad Knutson
Using Gravatar Images

WordPress uses Gravatar to display post author and comment author images. Gravatar is free, easy to set up, and widely used across the web. It’s a great way to show people who you are when commenting on blogs and sites.

From a web development standpoint, we can utilize Gravatar’s images anywhere. All we need is an email address. Gravatar takes the email and uses an md5 hash to turn it into a string and append it to the URL.

Let’s look at a PHP function we can use anywhere, WordPress or otherwise, to turn an email address into an image.

	function gravatar_image($email, $size) {
		$gravatar = '' . md5(strtolower(trim($email))) . '?s='. $size .'';
		return $gravatar;
	echo '<img src="' . gravatar_image("",96) . '" />';

This is the Gravatar function at its simplest level. We take in an email address and a size, and turn it into an image. For an example of this, check out my author bio box at the bottom of this post. The image of myself is generated by Gravatar.

Gravatar Default Image

As cool as this is, Gravatar isn’t used by everyone, and there is a significant chunk of emails that don’t have an image tied to them. Because of this, perhaps we want a generic image to show when the email address isn’t registered through Gravatar yet.

If an email is not registered, by default Gravatar shows their own logo. But what if we want to show our own default image, perhaps a silhouette like WordPress? We can certainly do that by passing along a default (“d”) variable.

	$email = "";	
	$size = 96;
	$default = "";
	$gravatar = "" . md5(strtolower(trim($email))) . "?d=" . urlencode($default) . "&s=" . $size;
	echo '<img src="' . $gravatar . '" />';

Very cool – now we can personalize how our Gravatar images display. Gravatar actually provides another customization option, the “rating” variable. Gravatar allows users to register their email and choose Gravatars based on rating – ranging from “G” to “X”. If you only want “G” rated images to display on your site, add the &r=g string to the end of your URL.

If you’re using WordPress, and you want to show the author’s Gravatar in an author box, we can use the get_author_email() WordPress function and combine it with the technique we learned above.

	$gravatar = '' . md5(strtolower(trim(get_the_author_email()))) . '?s=96';

	echo '<img src="' . $gravatar . '" />';
The following two tabs change content below.
Founder at Inbounderish
Brad Knutson is a Web Developer in the Twin Cities area of Minnesota. He has experience working with WordPress and Drupal, and also has an interest in SEO and Inbound Marketing.

Keep Up-to-Date



See a complete list of topics discussed in blog posts here.

Check These Out

Get 2 Weeks Free! Sign Up Today! Premium Managed WordPress Hosting Genesis Framework for WordPress SEO is complex. Tools should be simple. Thesis Theme for WordPress:  Options Galore and a Helpful Support Community

Share Your Thoughts

Your email address will not be shown.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">