How to Change the Default WordPress Gravatar Image
Posted on Updated onBy default, WordPress has a neat little feature that allows users to create their own personal icons identifying them when they leave a comment. They do this by signing up at gravatar.com with their email ID and WordPress checks their database whenever someone writes a comment. At the same time, many commenters would not have signed up with gravatar.com and so WordPress has the ability to assign a default gravatar to these individuals. If we go into the “Settings” section of the WordPress dashboard and select “Discussion”, we can scroll down to the “Default Avatar” area and choose what kind of image we want to leave for them.
You can see in the screenshot below that the default gravatar appears next to unknown commenters:
There are a number of services and algorithms that create randomly generated gravatars linked to a person’s specific email ID. But wouldn’t you like to take this opportunity to brand your blog further by creating a unique and memorable gravatar for all commenters who don’t have one of their own? Most blogs simply go with the default “Mystery Person”. So if yours is something different, it helps differentiate you even more.
WordPress has a handy filter that allows us to insert our own image into a particular array, thereby giving us the ability to switch our default gravatar to an image of our choosing. Let’s take a look at how to do this.
Find and Place the New Gravatar
The first step is to create or download the new avatar you want. If you (understandably) don’t want or can’t create your own, you can visit a website like http://www.avatarsdb.com/ to find one that suits your taste. What’s amazing is that you can even download animated GIF images for your gravatar which will work perfectly fine with the technique I’m about to show you.
For my example, I downloaded an animated GIF called “Cat Rain” and saved it as a “.gif” file on my desktop. Now we need to decide where in the WordPress blog structure are we going to store it. I wouldn’t advise saving it in the themes folder since those can get overwritten on updates. Personally, I prefer to store it in the “wp-content” folder directly. This has the added benefit of being accessible via the WordPress function “content_url()”.
So open up your FTP program and upload your new default gravatar image to the “wp-content” folder – or any other location that you see fit.
Now comes the fun part. Open up your functions.php file or any other location where you place custom PHP and paste in the following code:
// Add our own gravatar function my_new_cat_gravatar( $avatar_defaults ) { $new_avatar = content_url().'/cat_rain.gif'; $avatar_defaults[$new_avatar] = "Cat!"; return $avatar_defaults; } add_filter( 'avatar_defaults' , 'my_new_cat_gravatar' );
Over here, we make use of the filter called “avatar_defaults” and redirect it to our own function. Then we link our image to a variable, add that variable to the “avatar_defaults” array and return it. Note that if you stored your Gravatar image in a different location, you’ll need to change the third line to point to your custom location. Finally, the fourth line gives the handle of the gravatar. Mine is “Cat!”, So you’ll probably want to change that too.
Save this code and reload the “Discussion” settings page we saw earlier. If all has gone well, you should see a new option at the bottom with the name of your custom gravatar as shown here:
Save your settings. The next time you load a post with comments by those who don’t have their own gravatar, you should see your custom image show up next to the name.
<
p style=”text-align: center;”>
As I said before, this even works with animated GIF images so that’s pretty cool!