How to Use Custom Gravatars Icons in WordPress

How to customise the default Gravatars in Wordpress 2.7 using built in WordPress filters to show your own set of custom gravatars images.

By Tim TrottWordPress • December 27, 2008
How to Use Custom Gravatars Icons in WordPress

When the feature to display user avatars, known as Gravatar support, was added to WordPress, the default "mystery man" was the only graphic available unless the commenter had previously signed up for Gravatar.

Soon after, there was an option to specify a default image, followed by the Identicon, Wavatar and MonsterID. In WordPress 2.7, the new comments overhaul does not allow custom Gravatars to be defined since they are all wrapped up into wp_list_comments.

Let's look at the original method for showing Gravatars:

php
if(function_exists('get_avatar')) 
{
  echo get_avatar($comment, $size = '32', $default = get_bloginfo('template_directory') . '/images/gravatar.png');
} 

This was a nice, simple template function where you pass in the comment (from within the loop), the size for the avatar in pixels, and a filename for the default image.

We must use a different approach since we can no longer directly call to get_avatar. But don't worry, the good guys at WordPress have made it easy for us with a nice filter we can hook into.

php
function addgravatar($avatar_defaults) 
{
  $avatar = get_bloginfo('template_directory') . '/images/gravatar.png';
  $avatar_defaults[$avatar] = 'Tutorial Gravatar';
  return $avatar_defaults;
}
add_filter('avatar_defaults', 'addgravatar');

The filter, aptly named avatar_defaults, is a powerful tool. It allows you to add a new filename to the array of available filenames, giving you the flexibility to customize your avatars. This example assumes that the image is called gravatar.png and is located inside the images folder of your current theme. If this isn't the case, you can easily amend the filename. We also need to give the avatar a name; in this case, I called it "Your Paranormal" as this is the site I am writing it for. The last line adds the new function to the filters list.

When you go into the WordPress administration screen under Discussion, you will see your new default avatar listed, which will be selectable. It's a straightforward process that you'll master in no time.

Wordpress custom gravatars
Wordpress custom gravatars

Isn't that nicer than a hard-coded hack in the theme? A hard-coded hack refers to a modification made directly to the theme's code, which can be difficult to maintain and update. The new method, using the 'avatar_defaults' filter, allows for a more flexible and manageable way to set default avatars.

About the Author

Tim Trott is a senior software engineer with over 20 years of experience in designing, building, and maintaining software systems across a range of industries. Passionate about clean code, scalable architecture, and continuous learning, he specialises in creating robust solutions that solve real-world problems. He is currently based in Edinburgh, where he develops innovative software and collaborates with teams around the globe.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

This post has 5 comments. Why not join the discussion!

New comments for this post are currently closed.