Check whether a Gravatar exists for a specific e-mail

Ralph J. Smit Laravel Software Engineer

Gravatar is a great tool for getting avatars and profile images associated with a specific e-mailaddress. By default, if somebody has specified an image for their e-mail, that image can easily be retrieved. If the user doesn't have an e-mail, Gravatar will return a standard image.

But what if you don't want that default image? Gravatar has no easy option to check whether an image exists. WordPress has a function for this, called has_avatar(), but that function is not much of a use, because it also returns true when Gravatar gives you the standard image.

Fortunately, there's a smart way in PHP to check if a Gravatar exists for a specific e-mailaddress. The method could come in very handy if you're in WordPress and want to output comments in a custom format.

Anyway, let's get started!

Function to validate an e-mail

To check whether a Gravatar exists, we'll write a function that can be reused. The function is called validate_gravatar($email) and it takes the e-mailaddress needed to validate as a parameter.

The function sends a request to the Gravatar database. If the request returns a custom avatar, the $has_valid_avatar will be set to true. If not, the $has_valid_avatar will be set to false.

Finally, the function returns the value of the $has_valid_avatar variable. This means that the function output will always be a simple boolean.

function validate_gravatar($email) {
// Craft a potential url and test its headers
$hash = md5(strtolower(trim($email)));
 
$uri = 'http://www.gravatar.com/avatar/' . $hash . '?d=404';
$headers = @get_headers($uri);
 
if (!preg_match("|200|", $headers[0])) {
$has_valid_avatar = false;
} else {
$has_valid_avatar = true;
}
 
return $has_valid_avatar;
}

You should place the above function in the file you need to use it in. So if you're using it in functions.php, place it there.

Using the validate Gravatar e-mail with the WordPress comments - example

Now that we have a handy function to check if a Gravatar exists, how do we use it? I like to define the following three variables when I want to use this in WordPress when I'm creating a custom WordPress comments walker.

First, we'll get the email of the comment author. Then, we'll check whether that person has his own Gravatar. Now you can use the $rjs_has_gravatar variable with a simple php if-statement. Last, we'll get the avatar and store it in a variable.

$rjs_comment_email = get_comment_author_email();
$rjs_has_gravatar = validate_gravatar($rjs_comment_email);
$rjs_gravatar = get_avatar( $rjs_comment_email, 160 );

Note: the third step is optional. I like to do it like this, because it makes the code more clear.

If you've declared those three variables, you could use them like this to display the avatar <img> tag. The get_avatar() function gets that tag (function in Codex).

<?php if ($rjs_has_gravatar) { echo $rjs_gravatar; } ?>

You could also use it to show or hide the <img> tag plus a container.

<?php if (get_option('show_avatars', true) && $rjs_has_gravatar) : ?>
 
<div class="rjs-comment-authorimage"> <!-- Container -->
<?php if ($rjs_gravatar) { echo $rjs_gravatar; } ?> <!-- <img> tag -->
</div> <!-- Container closing tags -->
 
<?php endif; ?>

Conclusion

As you see, it's very easy to check with PHP and/or WordPress whether there exists a Gravatar. The technique can be used both on WordPress and non-WordPress situations, so it's very versatile. If you're using this code in a non-WordPress situation, make sure to check whether the functions used in the examples are available.

Published by Ralph J. Smit on in Guides . Last updated on 11 March 2022 .