If you do not yet have a WordPress Gravatar, you can get one at www.gravatar.com. If you have your own blog, it makes sense to get a WordPress Gravatar because your gravatar image will automatically follow you on all websites that have gravatar support. This helps to propagate your online image or online visual brand.
Gravatar support is automatically included in WordPress blogs since release 2.5. By using the get_avatar function in your comments.php file, you can get and post the gravatars of comment authors on your blog. In fact, most modern WordPress themes will automatically provide support for gravatars.
However, if a comment author does not have a gravatar image, there are a small number of gravatar options available. Go to Settings >>Discussion on your WordPress dashboard and scroll to the bottom of the screen. At the bottom of the Settings >> Discussion screen, is an Avatar panel that lets you set Avatar Display, Maximum Rating, and Default Avatar. The Default Avatar menu lets you pick what graphic to show in the event that a gravatar image cannot be retrieved for a comment author.
If you want to set the gravatar image to your own default graphic, then simply include that graphic in your get_avatar function call, which is most likely in the comments.php file of your current theme.
get_avatar( $comment, 64, 'http://www.example.com/default_gravatar.jpg' );
Things get a bit more complex when you want to generate a variety of default images based on the comment author’s email or IP address. Here we describe how you can use your own default avatar images based on the comment author’s IP address.
1. Determine your set of avatar images
Get all your default avatar images together, and transfer them to your theme directory or to the wp-content directory of your blog. Make sure that your avatar images have equal width and height.
Copy and paste the get_images and get_gravatar_images functions below into the functions.php file of your WordPress theme.
The get_images function uses optimized code from Matt Mullenweg’s image randomizer script to extract all image files from a given directory.
The get_gravatar_images function uses the get_images function to query your local gravatar directory, and create a list of all your default avatar image files.
// Code snippet slightly modified from http://photomatt.net/scripts/randomimage function get_images($folder) { // Space seperated list of extensions, you probably won't have to change this. $exts = 'jpg jpeg png gif'; $str = ''; $i = -1; // Initialize some variables if ('' == $folder) $folder = './'; $handle = opendir($folder); $exts = explode(' ', $exts); while (false !== ($file = readdir($handle))) { foreach($exts as $ext) { // for each extension check the extension if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive $str .= $file; if ($str) $str .= '|'; ++$i; } } } closedir($handle); // We're not using it anymore return $str; } function get_gravatar_images() { // Get all gravatar images $str = get_theme_mod('gravatar_img', FALSE); if ($str !== FALSE) return $str; $dir = WP_CONTENT_DIR . '/themes/shiba/images/gravatar/'; if (is_dir($dir)) { $str = get_images($dir); } else { return ''; } set_theme_mod('gravatar_img', $str); return $str; }
Lines 28-29 – Checks to see if your avatar list has previously been generated. If so, use the previous list. In this way, we only need to generate our avatar image list once.
Line 31 – Make sure to enter the name of your gravatar directory. WP_CONTENT_DIR already points to the wp-content directory of your blog, so you only need to enter your gravatar path relative to wp-content.
Lines 33 – Creates a list of all image files within your gravatar directory.
Line 38 – Stores your list of avatar files for quick, future retrieval.
2. Assign avatar images based on comment author attribute
Once you have your default avatar images in place, you can assign them to comment authors based on their e-mail or IP. The get_my_avatar function below assigns default avatar images to your comment authors using their IP address.
function get_my_avatar($comment, $dim) { static $gravatar_img = NULL; $img_uri = get_avatar($comment, $dim); $uri = substring($img_uri, "src='", "s="); $headers = wp_get_http_headers($uri . "d=404"); // 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; if ($has_valid_avatar) return $img_uri; // Not valid avatar - return a default based on IP $img_num = intval($comment->comment_author_IP); if (!$gravatar_img) { $gravatar_img = get_gravatar_images(); if (!$gravatar_img) return $img_uri; $gravatar_img = explode('|', $gravatar_img); } $img_count = count($gravatar_img); $index = abs($img_num % $img_count); // Construct new image http $img_uri = "<img src='" . get_bloginfo('template_url') . '/images/gravatar/' . $gravatar_img[$index] . "' width='" . $dim . "' height='" . $dim . "' />"; return $img_uri; }
Lines 4-17 – Check gravatar.com to see if the comment author has a valid gravatar image. If so, use that image.
Lines 20-31 – If comment author does not have a registered gravatar image, then assign a local default avatar image to the comment author based on his IP address. You can also use e-mail or other comment author attributes.
The local default avatar image used is taken from your local gravatar directory, which you previously retrieved using the get_gravatar_images function.
Line 31 – Make sure you use your own local gravatar directory here.
By using this code, you can use your own blog avatars rather than be limited to those provided to you by WordPress. If you are unsatisfied with Identicon, Wavatar, or MonsterID – then just assign your own avatars using the code above. Have fun!
Tom says
Hey,
thank you for the nice script. Unfortunately it doesn’t work at my site. Either the the first snipet (with 1 default avatar) or the random-avatar-script.
Is there anything more to do to edit the path to the own avatar-directory?
My comments shows still the “mystical person”-avatar 🙁
Rio says
Hi,
Is there any way to assign certain avatar based on role when a user register?
For example, a picture of person for User role, a pen for Editor, a computer for Admin, etc.
Or.., to assign a picture to roles (along with the user avatars).
The purpose is to see in a glance what role (or level) a user has.
Thank you
ShibaShake says
No, currently there is no in-built functionality to do automatic image overlays.
Matthew Spence says
The idea of asking Gravatar if a gravatar was available before the page had responded seemed really bad as it slows down response time. I problem I think you’ve blogged about and solved with your avatar plugin that caches them.
I wrote a modification that means the default avatar is always assigned based on IP and passed to Gravatar as the default image. It doesn’t solve the problem of Gravatar being slow, but it means that slowness is in the image requests not in the all important page request.
http://digitalintuition.tumblr.com/post/5637832013/how-to-multiple-default-avatars-assigned-to-authors
John says
I cant seem to get it working on my site…
ShibaShake says
The more details you can provide me the better I can help debug it.
For example, which part is not working? Can you install? Can you activate? Can you go into Comments >> Cache Gravatar? What happens when you click on Cache Gravatars? etc.
alex says
thank U for information, but it is a little difficult. Is there to use some plugin to use own gravatrs on web?
thanks!
ShibaShake says
http://shibashake.com/wordpress-theme/wordpress-gravatar-plugin