How to Change the Default Gravatar in WordPress
As much as I love seeing the mystery man appear in WordPress comments across the internet, your blog deserves better. In this tutorial we’ll take a custom image and add it as the default gravatar to give your comment section a more personalized touch.
The Goal
If you still haven’t been acquainted with the mystery man icon, here’s a short introduction:
![]()
Now that you’ve been properly introduced, let’s get rid of him. We’ll do this by adding more options for defaults. The solution in this example is much more elegant than simply replacing the default image. The result will be much more customized and will give you options straight from the WordPress 2.7 dashboard under Settings>Discussion.
![]()
Gravatar Code for functions.php
Each WordPress theme has the option of including functions.php. This file is responsible for loading any additional options for the theme in the WordPress dashboard. The default Kubrick theme for instance, would use functions.php to change the header colors. We’ll be using it to specify a brand new avatar for use with the current theme. Start by opening up the functions.php file in your theme directory. If you do not see one, make it now. Depending on your current theme, there may already be some code involved. Ignore it, our code will be independent from the rest of the code.
add_filter( 'avatar_defaults', 'newgravatar' );
function newgravatar ($avatar_defaults) {
$myavatar = get_bloginfo('template_directory') . '/images/buildinternet-gravatar.jpg';
$avatar_defaults[$myavatar] = "Build Internet";
return $avatar_defaults;
}
It’s actually a relatively simple function. You’ll only need to change the image location and avatar title to your own needs. For those interested in the full process, here’s a more specific breakdown of what’s going on:
- The add_filter function says that we’ll be editing the avatar_defaults array using our custom function “newgravatar”
- The newgravatar function is responsible for providing the location and title of our additional gravatar option
- The first line in the function points to the new gravatar in the images folder of the template directory
- The second line designates the name that will be displayed next to the image dashboard options.
- The third line sends back the results to the avatar_defaults array to be included in the theme
Goodbye Mystery Man
It’s easy to add or change existing gravatars using this snippet of code. If you’re looking to have a couple options at once, simply duplicate the lines in the function with “$myavatar” and then rename the variable. Now there is absolutely no excuse for letting the Mystery Man into your comment section ever again. He’s had a good run, but his retirement is long overdue.



