Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
In wordpress, a better way to check if an author has a gravatar or not. Sometimes you might want to check to see if a gravatar exists and not display any image if there isn't one. Uses the wordpress HTTP and caching apis. A better version of this: http://codex.wordpress.org/Using_Gravatars#Checking_for_the_Existence_of_a_Gravatar
/**
* Utility function to check if a gravatar exists for a given email or id
* @param int|string|object $id_or_email A user ID, email address, or comment object
* @return bool if the gravatar exists or not
*/
function validate_gravatar($id_or_email) {
//id or email code borrowed from wp-includes/pluggable.php
$email = '';
if ( is_numeric($id_or_email) ) {
$id = (int) $id_or_email;
$user = get_userdata($id);
if ( $user )
$email = $user->user_email;
} elseif ( is_object($id_or_email) ) {
// No avatar for pingbacks or trackbacks
$allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) )
return false;
if ( !empty($id_or_email->user_id) ) {
$id = (int) $id_or_email->user_id;
$user = get_userdata($id);
if ( $user)
$email = $user->user_email;
} elseif ( !empty($id_or_email->comment_author_email) ) {
$email = $id_or_email->comment_author_email;
}
} else {
$email = $id_or_email;
}
$hashkey = md5(strtolower(trim($email)));
$uri = 'http://www.gravatar.com/avatar/' . $hashkey . '?d=404';
$data = wp_cache_get($hashkey);
if (false === $data) {
$response = wp_remote_head($uri);
if( is_wp_error($response) ) {
$data = 'not200';
} else {
$data = $response['response']['code'];
}
wp_cache_set($hashkey, $data, $group = '', $expire = 60*5);
}
if ($data == '200'){
return true;
} else {
return false;
}
}
@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Nov 3, 2014

This is a great and useful function, thank you for sharing.

@sanslan

This comment has been minimized.

Copy link

@sanslan sanslan commented Aug 10, 2016

Thanks.

@AjGabriel

This comment has been minimized.

Copy link

@AjGabriel AjGabriel commented Sep 23, 2016

Hello! This is a great function however I am encountering a problem because the gravatars are not showing up. Is there any modification that has been made? Sorry I am still a newbie.

@AjGabriel

This comment has been minimized.

Copy link

@AjGabriel AjGabriel commented Sep 23, 2016

I using the function to validate gravatar if the email exist or not. Let me know if you want to view my code... Please help. Thanks!

@mknee123

This comment has been minimized.

Copy link

@mknee123 mknee123 commented Feb 16, 2017

Yes, THANK YOU!

@kousherAlam

This comment has been minimized.

Copy link

@kousherAlam kousherAlam commented Feb 23, 2017

wow , it is very useful.

@cstuart

This comment has been minimized.

Copy link

@cstuart cstuart commented May 25, 2018

Legend!!! Thank you!!

@Dibbyo456

This comment has been minimized.

Copy link

@Dibbyo456 Dibbyo456 commented Oct 6, 2018

Why use wp_cache instead of wp_transients ?

By default, the object cache is non-persistent. This means that data stored in the cache resides in memory only and only for the duration of the request. Cached data will not be stored persistently across page loads unless you install a persistent caching plugin.

@tdrdimov

This comment has been minimized.

Copy link

@tdrdimov tdrdimov commented Jun 12, 2020

Well done, BRAVO!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.