|
|
View previous topic
::
View next topic
|
Author |
Message |
ramon fincken Site's programmer
 Get a free globally recognized avatar It's free!
Joined: 03 Aug 2007 Posts: 412 Location: A'dam/Diemen, The Netherlands
|
Posted: Sun May 23, 2010 4:32 pm Post subject: [solution][snipplet] function has_gravatar() |
|
|
Looking for an elegant method to see if a user has a gravatar or not ..
I stumbled upon this url: http://codex.wordpress.org/Using_Gravat...a_Gravatar
> Quote: | The trick to do this is to specify "404" as the default. In this case, the gravatar service will return a 404 error if no gravatar exists, instead of returning some default image. A real image will get a 200 code. It is best to check for 200, as some other errors might be returned as well, for other cases. |
Code: |
function validate_gravatar($email) {
// Craft a potential url and test its headers
$hash = md5($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;
} |
Works great, alltough I would have named the function has_gravatar($email); or user_has_gravatar($email);
So this is the code I use at http://www.ikzoekeensportpartner.nl :
Warning: you cannot simply copy paste this code below, but if you are a PHP coder, you'll know what to do
Code: |
/**
* Checks if user has a gravatar or not
* @return boolean
*/
function has_valid_gravatar($email) {
// Craft a potential url and test its headers
$hash = md5($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;
}
/**
* Returns gravatar OR default avatar url (if no gravatar found)
*/
function create_gravatar_url($email, $size) {
$rating = 'R';
global $default_gravatar;
if (validate :: email($email)) {
if (overall :: has_valid_gravatar($email)) {
return $url = "http://www.gravatar.com/avatar/" . md5($email) . "?r=" . $rating . "&default=" . $default_gravatar . "&size=" . $size;
}
}
return $default_gravatar;
} |
|
|
Back to top |
|
 |
Google adsense Advertisement
|
Posted: Sun May 23, 2010 4:32 pm Post subject: [solution][snipplet] function has_gravatar() |
|
|
Advertisement
|
|
Back to top |
|
 |
GravityForms Advertisement
|
Posted: Sun May 23, 2010 4:32 pm Post subject: [solution][snipplet] function has_gravatar() |
|
|
Advertisement
 |
|
Back to top |
|
 |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|