Checking for Avatars & Gravatars in BuddyPress


Building a new BuddyPress site for a client, I found I needed to echo certain content based on whether or not a user:

  1. had uploaded an avatar to illustrate his/her profile;
  2. had associated a Gravatar with his/her profile email;
  3. or, finally, had done neither.

My client explicitly articulated the importance of illustrating its community profiles with (Gr)avatars, and in case a user account didn’t satisfy one of the first two options, I wanted to echo a prominent link that encouraged the account holder to visit the “Change Avatar” section of his/her profile and remedy this.

Until a plugin presents itself, or Automattic and/or the BuddyPress/WordPress communities integrate a specific custom profile field for (Gr)avatars into the networking framework — thereby allowing developers and site managers to require, and determine the visibility of, profile images via the BuddyPress/WordPress user admin UI — my solution follows.

First, drop the following function into your BuddyPress theme’s functions.php file:

function validate_gravatar($email) {
$hash = md5($email);
$uri = '' . $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;

Second, drop the following conditional statement and list of variables where you want the custom content to appear:

$avatarcount = 0;
if ( is_user_logged_in() ) {
global $bp;
global $current_user;
$local = BP_AVATAR_UPLOAD_PATH . '/avatars/' . $bp->loggedin_user->id;
$useremail = $current_user->user_email;
if (file_exists($local) ) {
echo 'Avatar exists';
elseif (validate_gravatar($useremail)) {
echo 'Gravatar exists';
else { echo 'Neither avatar nor Gravatar exist.';

The statement first queries the BuddyPress avatar upload directory (.../wp-content/uploads/avatars/), and, if it finds a folder named with the current user’s id (e.g., .../wp-content/uploads/avatars/34/), it echoes the first message (in this case, “Avatar exists”). BuddyPress creates this folder during the avatar upload process, and querying it, instead of attempting to query the individual avatar file(s), is good practice for several reasons.

First, BuddyPress dynamically generates avatar image filenames, which makes anticipating them in advance quite difficult. Second, if you want a given bit of content to change or, in my case, disappear immediately after upload, this method allows for that before, rather than after, the image crop sequence.

After checking the BuddyPress avatar upload directory, the statement uses the validate_gravatar function to see if the user’s email address is associated with any Gravatar. If it is, as written above, BuddyPress echos the statement “Gravatar exists.” If it isn’t, and the first condition also hasn’t been met, the statement echos the final bit of content, in this case, “Neither avatar nor Gravatar exist.”

Finally, it wasn’t terribly important for my purposes, but, in case others find it is, the three messages will only appear for users who are logged in (per the if ( is_user_logged_in() ) { ... } conditional). Removing this wrapper will display the messages publicly, for all visitors — those logged in, those not logged out; site members and at-large visitors — which some users might also find appealing.

While framing and composing the above statement, I found the following BuddyPress community articles quite helpful:

  1. How to tell if a person has uploaded a avatar only once?
  2. How to check if member has avatar?

And WordPress user scole_direct‘s reference to the validate_gravatar function, outlined in the WordPress Codex’ “Using Gravatars” article, was similarly useful, for obvious reasons.

Get the most out of your online marketing efforts

Set Your Words on Fire

    Studio Hyperset’s master guide to content, platforms, and the contemporary marketing pipeline.

Quimby Melton


Quimby is the founder and president of Studio Hyperset. He started Studio Hyperset in 2006 after studying Transatlantic literature and culture, new media, and humanities computing at the Universities of Georgia (AB, 00) and Nevada, Las Vegas (MA, 03; PhD, 08). He has a background in literary studies, media production, front-end development, and project management. His current primary duties involve helping clients frame solutions that meet their needs; scoping and managing Studio Hyperset's growth strategy and business development pipeline; finding and developing talent; and, most importantly, supporting the SH team.