Web Design that taps into the haromny and vision of your dreams.

Custom Gravatars in Wordpress

How to add your own Gravatars for comment pictures

By on in Coding

318 words, estimated reading time 2 minutes.

A quick tip for customising the default Gravatars in Wordpress 2.7 now they took away the get_avatar function. Use this filter instead; it's a much better way!

When Gravatar support was added in Wordpress the default "mystery man" was the only graphic available unless the commenter had previously signed up for gravatar. Soon after that, 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.

Lets look at the original method for showing gravatars:

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.

Since we can no longer make a direct call to get_avatar, we must use a different approach. Thankfully the good guys at Wordpress made a nice filter that we can hook into.

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 is called avatar_defaults and can be used to add a new filename to the array of available filenames. 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 simply 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.

Now when you go into the Wordpress administration screen under Discussion you will see your new default avatar listed and it will be selectable.

Wordpress custom gravatars
Wordpress custom gravatars

There, isn't that nicer than a hard coded hack in the theme?

Last updated on: Thursday 22nd June 2017

 

Comments

Have a question or suggestion? Please leave a comment to start the discussion.

Sandrine

Sandrine

Great !! Thanks for sharing, it's very useful.

Reply to Sandrine
Ben

Ben

Where is the best place to add this filter? functions.php?

Reply to Ben
Tim Trott

Tim Trott

Yes, functions.php is the best place to place the code.

Reply to Tim Trott
Matt Algren

Matt Algren

Thanks for the tip! You might want to warn people that if they cut/paste your solution they'll have to manually replace all the curly apostrophes for normal ones.

Not that I just spent an hour pulling my hair out. Not me.

No siree.

Reply to Matt Algren
Tim Trott

Tim Trott

Very good point Matt, thanks for pointing it out.

Reply to Tim Trott

 

Leave a Reply

Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Your email address will not be published.