If you're experienced with programmatically creating pages, you may wish to skip down to the code.

I’ve been working on a project where the requirements simply stated this:

  • If the user has a gravatar, display it
  • Otherwise, give the user the ability to upload a profile image from their computer

The WordPress API offers the get_avatar function for easily retrieving a users gravatar image, but there’s currently no API function that allows us to check if a user has a gravatar.

When it comes to writing helper functions, I try to make sure that the function’s signature reads as closely to English as possible. Of course, it’s still code so there’s only so far we can go, right?

Anyway, in order to check if the current user has a gravatar, I wanted to write a function that read something like this:

if( has_gravatar( wp_current_user()->user_email ) {
	// Display the gravatar
} else {
	// Display the form to upload an image
} // end if/else

Check if a User Has a Gravatar

In order to do this, three things need to happen:

  1. An MD5 hashed version of the email address needs to be passed to the Gravatar API
  2. The headers returned from the URL will need to be read
  3. If a 404 is returned, then the user does not have a gravatar

Based on those requirements, here’s the helper function I wrote to check if a user has a gravatar in WordPress:

/**
 * Checks to see if the specified email address has a Gravatar image.
 *
 * @param	$email_address	The email of the address of the user to check
 * @return			          Whether or not the user has a gravatar
 * @since	1.0
 */
function example_has_gravatar( $email_address ) {

	// Build the Gravatar URL by hasing the email address
	$url = 'http://www.gravatar.com/avatar/' . md5( strtolower( trim ( $email_address ) ) ) . '?d=404';

	// Now check the headers...
	$headers = @get_headers( $url );

	// If 200 is found, the user has a Gravatar; otherwise, they don't.
	return preg_match( '|200|', $headers[0] ) ? true : false;

} // end example_has_gravatar

From here, it can be called almost exactly as defined earlier in the post. The function can actually be simplified a bit, but it would decrease readability. Ultimately, it’s your call.

Just remember to replace example with whatever the name of your theme or plugin is in order to follow WordPress coding standards.

Category:
Tips
Tags:

Join the conversation! 9 Comments

  1. Guess, in addition that function can be used as a starting point to retrieve further profile info from one’s Gravatar profile, such as the About Me field?

    • You could; however, that code could would probably be better suited for its own function – or you could just fall back to the_author_meta as it may provide exactly what you’re looking for.

      • or you could just fall back to the_author_meta

        I had been planning to access profile info from a Gravatar profile, not a WP user profile. But yes, the_author_meta is where I settled after finding out that the Gravatar API only will deliver profile information for the primary email address of a user profile. Since there’s no way to check if a comment author is using their primary or secondary or whatever-ary email address from Gravatar, trying to access more data from their profile seems pointless as the chance of a NULL response becomes more likely.

  2. Thanks for sharing this awesome snippet.

  3. I was doing something like this but using instead of using get_headers(), I was using wp_remote_head(). Both approaches are terribly slow so I’m investigating if there’s a quicker way of checking if a Grav exists.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.