QNimate

  • CoursesVideos
  • WP PremiumPlugins
  • DemosLab
  • Home
  • QIdea
  • QTrack
Home Carbon Ads WordPress Check if Gravatar Exists

WordPress Check if Gravatar Exists

Here is a function which takes an email address and checks if an gravatar exists or not.

function validate_gravatar($email)
{
    // Craft a potential url and test its headers
    $hash = md5(strtolower(trim($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;
}

If gravatar exists then it returns true otherwise false.

Here is an another function which checks if gravatar exists or not. This function uses HTTP API and Caching. Its a better method than the above one. This method is more WordPress while the above method is more PHP.

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;
    }
}

Returns true if gravatar exists otherwise false.

Mar 11, 2015Narayan Prusty
Change WordPress Default AvatarGet HTML5 Audio Duration Length
Comments: 2
  1. Melvina
    6 years ago

    An intillegent point of view, well expressed! Thanks!

    ReplyCancel
  2. Paal Joachim Romdahl
    6 years ago

    Actually here it would be good to also offer a simple media browser button in the profile screen so the user can upload or browse the media library for their own avatar. Either use the Gravatar or use an avatar.
    I know there are plugins for this but it would also be a way to extend the above tutorial. Thanks.

    ReplyCancel

Leave a Reply Cancel reply

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

Narayan Prusty

I am a software engineer specialising in Blockchain, DevOps and Go/JavaScript. This is my personal blog where I write about things that I learn and feel interesting to share.

8 years ago 2 Comments WordPress
Share this
0
GooglePlus
0
Facebook
0
Twitter
0
Linkedin
Related Articles
  • How Does WordPress Cache Data?
  • WordPress $notoptions Array
  • wp_load_alloptions() Function
  • WordPress Remove Avatar from Comments Only
  • Change WordPress Default Avatar
Our Sponsor
My Books

2014 - 2015 © QNimate
All tutorials MIT license