Support » Plugins » [Plugin: Add Local Avatar] Checking to see if user has avatar/gravatar

  • Hi,
    I like the plugin and wanted to go a step further in encouraging (not forcing) registered users to use an avatar (preferably a photo of themselves). So I wrote this little hack and stuck it in my theme’s function.php file. I call the function from my index.php and other template files (<?php echo avatarcheck() ?>), styling the messagebox in style.css.

    So for what it’s worth:

    function validate_gravatar($email) {
    	// From http://codex.wordpress.org/Using_Gravatars
    	// 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;
    	}
    
    function avatarcheck() {
    	$avatarcount = 0;
    
    	// Only perform avatar check for logged in users
    	if ( is_user_logged_in() ) {
    		global $current_user;
    		get_currentuserinfo();
    		$userid = $current_user->ID;
    		$useremail = $current_user->user_email;
    
    		if(validate_gravatar($useremail)) {
    			// User has a Gravatar
    			$avatarcount++;
    			}
    
    		$local = get_usermeta($userid, 'avatar');
    
    		if(strlen($local)>0) {
    			// User has a local avatar
    			$avatarcount++;
    			}
    
    		if ($avatarcount==0) {
    			// No local avatar or gravatar -- Display a message box encouraging user to add one
    
    			$avatarmsg = '<div id="gravatarmsg">';
    			$avatarmsg .= "Hey ".$current_user->user_firstname.": ";
    			$avatarmsg .= "You don't seem to have a profile picture yet. Profile pictures help identify each other in the real world and encourage positive dialogue between users of the website (when posting comments for example). Please take a moment to upload a close-up photo of your face via <a href='http://www.yoursite.com/wp-admin/profile.php'>your profile page</a> (see the 'Avatar' section at the bottom of the profile page). Alternatively get a <a href='http://en.gravatar.com/' target='_blank'>'Gravatar'</a> profile picture that you can use on multiple sites.";
    			$avatarmsg .= "</div>";
    			}
    
    		}	
    
    		return $avatarmsg;
    	}

    http://wordpress.org/extend/plugins/add-local-avatar/

  • The topic ‘[Plugin: Add Local Avatar] Checking to see if user has avatar/gravatar’ is closed to new replies.