HowTo – Avatars Gravatars Adding Author Pics To Posts

Filed under: Wordpress Tips

wordpress_logoDepending on the type of blog or website that you run with wordpress you may want to include a small picture of the writer within their posts or maybe on an about page.

In wordpress to do this the developers at Automatic.com have setup a Avatar Hosting site to allow Authors that post to many different sites control their avatar from one place.

Gravatar is the default way to add avatars to your posts so there is no default internal way to manage your own Avatars. However Plugins could be used for this purpose.

Gravatars are default 96px X 96px images and you can resize the image that is sent back to you to meet your design needs.

Gravatar.com works not only with wordpress but also with most popular blog cms software. If your specific application does not support it then you can easily add this feature by using perl php or other code.

Adding Your Gravatar

In order to use gravatars you must turn them on in your control panel

The option is set under Settings – Discussion and there are a number of selections including suitability settings G PG X to make sure you are only returning appropriate Avatars on your site.

In your WordPress theme files you can add the following line of code within your Loop to display the Gravatar of the person who wrote the post.

<?php
   echo get_avatar( $id_or_email, $size = '96', $default = '<path_to_url>' );
?>

The Returned code you will get is

<img alt='' src='http://gravatarurl_or_default' class='avatar avatar-$size' height='$size' width='$size' />

Checking For Default Gravatars

If the person that wrote the post does not have a Gravatar account then requesting a gravatar will return a default image. You may not want the default Gravatar.com Image associated with your posts and to check for the availability of a gravatar you can use the following code.

function validate_gravatar($user_id) {
	// Mine is just for user_id, you can work out dealing with emails as well for yourself.
	$user = get_userdata($user_id);

	// Craft a potential url and test its headers
	$email = $user->user_email;
	$hash = md5($email);
	$uri = 'http://www.gravatar.com/avatar/' . $hash . '?d=identicon&r=any&size=80';
	$headers = wp_get_http_headers($uri);

	// Check the headers
	if (!is_array($headers)) :
		$has_valid_avatar = FALSE;
	elseif (isset($headers["content-disposition"]) ) :
		$has_valid_avatar = TRUE;
	else :
		$has_valid_avatar = FALSE;
	endif;
	return $has_valid_gravatar;
}

This code snipet came from WordPress.org Codex on Gravatar use


Adding a Gravatar in PHP

In PHP to add a Gravatar it is a little more difficult but still not hard.

First your CMS or Blog software should provide a way to store and return email addresses of your users. You need their email to retrieve their gravatar.

Next you need to setup the url for your gravitar image.

$email = "someone@somewhere.com";
$default = "http://www.somewhere.com/homestar.jpg";
$size = 40;

You can construct your gravatar url with the following php code:

$grav_url = "http://www.gravatar.com/avatar.php?
gravatar_id=".md5( strtolower($email) ).
"&default=".urlencode($default).
"&size=".$size;

Once you have tested your code implement it in your application and then grab the image from the gravatar.com site.

For more information or a Free Gravatar.com account visit

www.gravatar.com