Top Poster
#1 ramon fincken 397
#2 Site admin 3
#3 Ber|Art 2
#4 braddmark 1
#5 accentmedia 1
#6 Xarzu 1
#7 pEcosespedex 1
#8 khadish 1
#9 moxxx 1
#10 nistelrock 1
[solution][snipplet] function has_gravatar()



Rating : 3 / 5

 
Post new topic   Reply to topic    Ramonfincken.com Forum Index -> PHP coding
Beta
Want to be notified by email when this topic gets a reply?  
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: 397
Location: A'dam/Diemen, The Netherlands

PostPosted: Sun May 23, 2010 4:32 pm    Post subject: [solution][snipplet] function has_gravatar() Reply with quote

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 Smile
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
View user's profile Send private message Visit poster's website
Google adsense
Advertisement





PostPosted: Sun May 23, 2010 4:32 pm    Post subject: [solution][snipplet] function has_gravatar()

Advertisement
Back to top
GravityForms
Advertisement





PostPosted: Sun May 23, 2010 4:32 pm    Post subject: [solution][snipplet] function has_gravatar()

Advertisement
Gravity Forms Plugin for WordPress
Back to top
Post new topic   Reply to topic    Ramonfincken.com Forum Index -> PHP coding All times are GMT
Page 1 of 1

 
Jump to:  
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
General forums
The world of freelance
Programming
Other projects
Poll
I love gifts

Yes goodies and presents! [3]
No but I love polls ! [1]

Related google ads