How To Create a Custom Default Avatar For WordPress

Customize Default Avatars In WordPress CommentsFirst, I think anyone who owns a WordPress site or comments on sites that use Gravatars (the photos that show up next to comments and in author boxes etc.) should have a Gravatar account with their photo tied to their email address.

If you don’t have a free Gravatar account, head to http://gravatar.com, signup, and upload a photo.

For branding purposes, it is usually a good idea to use the same photo for your Gravatar as your Facebook, Twitter, and LinkedIn profiles.

If you own a WordPress site, under the discussion settings, there is a set of options regarding Avatars and one is to use a custom avatar.

Near the bottom of the Discussion Settings window, there are several options to display different avatars when a commenter on your blog does not have a custom gravatar tied to their email address.

I think these choices are ugly and boring, and often take away from the design of the site.

Custom Default Avatar for BlogsWith a little bit code added to your theme’s (or child theme) functions.php file, and a custom image uploaded via FTP to your theme’s image folder, you can create a new default option that matches your site.

Bourn Creative’s custom branded avatar is a version of our “brand rainbow.”

For a regular WordPress theme (like our custom themes) add the following code at the end of the functions.php just before the closing tag ?> (if there is one).

//**Custom Gravatar**/
add_filter( 'avatar_defaults', 'bourncreative_custom_gravatar' );
function bourncreative_custom_gravatar ($avatar_defaults) {
$myavatar = get_bloginfo('template_directory') . '/images/custom-gravatar.jpg';
$avatar_defaults[$myavatar] = "Custom Gravatar";
return $avatar_defaults;
}

For a child theme like a StudioPress theme that runs on the Genesis Framework you can use the following at the end of the functions.php file.

//***Custom Gravatar**/
add_filter( 'avatar_defaults', 'bourncreative_custom_gravatar' );  
function bourncreative_custom_gravatar ($avatar_defaults) {
     $myavatar = get_stylesheet_directory_uri() . '/images/custom-gravatar.jpg';
     $avatar_defaults[$myavatar] = "Custom Gravatar";
     return $avatar_defaults;
}

Next, no matter which method you use, you need to create your custom avatar image, name it the same as the function above (custom-gravatar.jpg), and then upload via FTP to your theme’s image folder.

Once you have done that, you can log in to your WordPress site and navigate to the Discussion settings and your custom avatar should be able to be set as the default.

The end result … a matching, branded avatar that is displayed for your blog commenters who do not have their own Gravatar.

WordPress Blog Comments Screen Shot of Custom Gravatar

As with all changes to your theme files; always backup first, work from backups, and test to be sure it works properly. Not every theme will be compatible.

About Brian Bourn

As CEO of Bourn Creative, Brian manages the business, the team, project workflow, and all custom web design projects. He’s basically in charge of the nerd stuff and specializes in website functionality, Star Wars, WordPress, and the Genesis Framework.

Genesis Framework by StudioPress

Opinions, feedback, and thoughts:

  1. Another good “how to” post for people who need to add an avatar (and many do)! It definitely helps the know/like/trust factor to be able to recognize someone’s avatar from site to site.
    Sue

    • Jennifer Bourn says:

      It amazes me to see how many people don’t have one! Especially people who have a blog and their own photo doesn’t even show up when they reply to commenters!

  2. Jennifer,

    Great, easy to follow instructions.

    Thanks for sharing.

    Write on!~

    Lisa

  3. Great tip Jennifer, can’t wait to have my own gravatar!

  4. Jennifer,
    Excellent and easy to use tips. What do you feel is the right thing to do on Facebook, twitter or LinkedIn when someone wants to connect and they show no photo?
    Thanks,
    Mitch

  5. Nice tip! That looks so much better than the default “shadow man”.

    On a slightly different note, I wish WP would support user avatars natively (w/o a plugin). I use Gravatar, but I do a lot of sites for clients that don’t necessarily need a Gravatar account.

    I’m going to try out your code on my site now! :)

    • I agree about the native avatars. I have used the plugin User Photo for local avatars for membership sites and other sites where most of the site users didn’t need or want a Gravatar account.

      Besides the custom Gravatar, we also create a custom branded favicon for each client site too.

  6. Typed in the following code and now I can’t access my wp admin or even the website?

    This error now appears but I can’t get back to change.

    Parse error: syntax error, unexpected T_STRING in /home/chrlan37/sprinttriathlontrainingforbeginners.com/wp-content/themes/standard/functions.php on line 3464

    • Hi Chris, You will need to access your site using an FTP program like FileZilla or Cyberduck and donload a copy of your theme. Once you have your theme downloaded you can open your functions.php file with a plain text editor like Notepad or TextEdit, delete the code you added, and upload the changed file to replace your existing one. That should get your site back up.

      It’s always a good idea to make code changes after downloading a backup of your theme first and working from a back up rather than through the theme editor in the WordPress Dashboard.

  7. Why is this not working? I have been trying everything. I am using a child theme and I am editing the child theme’s functions.php. Thanks for any feedback and suggestions.

    add_filter( ‘avatar_defaults’, ‘ife_custom_gravatar’ );
    function ife_custom_gravatar ($avatar_defaults) {
    $myavatar = get_stylesheet_directory_uri() . ‘/images/ife-gravatar.jpg’;
    $avatar_defaults[$myavatar] = “IFE”;
    return $avatar_defaults;
    }

    • Hi Seth, Your code is the same as mine so it may be due to copy & pasting. Sometimes the straight quotes get converted to curly quotes and then the function won’t work.

      Try copying mine from above and pasting into a text editor to be sure that you are using straight quotes.

      Also, be sure to select the custom Gravatar option from the settings page.

      • Yes, I am using straight quotes.This is my code copied from my text editor.

        add_filter( ‘avatar_defaults’, ‘ife_custom_gravatar’ );
        function ife_custom_gravatar ($avatar_defaults) {
        $myavatar = get_stylesheet_directory_uri() . ‘/images/ife-gravatar.jpg’;
        $avatar_defaults[$myavatar] = “IFE”;
        return $avatar_defaults;
        }

        I see the new choice under Settings > Discussion, but the image is not linking. When I inspect image, this is the HTML code.

        It seems to be searching gravatar for my image.

        • Code did not copy, not sure why. Here is the image code when inspecting…

          img alt=”" src=”http://1.gravatar.com/avatar/bf6e8ef427e1413f6f05b000491d082d?s=32&d=http%3A%2F%2Flocalhost%3A8888%2Fwp-content%2Fthemes%2Fstandard-child-theme-kit%2Fimages%2Fife-gravatar.jpg%3Fs%3D32&r=G&forcedefault=1″ class=”avatar avatar-32 photo” height=”32″ width=”32″

          • Got it. This is for people who don’t have an associated Gravatar with their email address. If someone has a Gravatar, it will show that image first.

            Have you tried it with a test email address that doesn’t have a Gravatar account?

          • I am the admin, trying to add a default gravatar under Settings > Discussion so I can select a custom image instead of Myster Man.

            Make sense?

          • Sorry I can’t be more help Seth. I use this function on every site we build and usually don’t have any issues with it.

  8. hi, it’s there any way to set the same avatar for all users without any plugin?
    at the moment i use one plugin for do that, but what can i do if i want to add the same avatar to all of my users me included without any plugin?
    how can i set this function on my function.php ?
    if someone know, please share experience
    thanx

  9. the problem is..i dont know nothing about write css
    if someone can give me the exact code..i will be happy forever ;)

  10. Chris Kidd says:

    Hi, I’ve tried this every way that I know also, and I am having the same problem as Seth above, in that the image itself is not showing up. In the admin panel where I set which default avatar to use, I see the name of the avatar that I used in the function, and I can also select it and save it, but no image is there.

    I am using a child theme functions file, so I don’t know what could be going wrong. Is there a certain size that the avatar has to be?

    I must be close because I see changes, just not the image (in the admin panel, or when I make a comment with a test e-mail address with no gravatar account).

    Thanks for any help.

    • Chris Kidd says:

      Hi again,
      I searched the code like Seth, and if I choose one of the ugly default avatars that come predefined in WP, it also points to gravatar as the source for the image.

      For example: On the “Retro” avatar, if it’s used as a default when a commenter does not have a gravatar account, this is what the source code is the retro default:

  11. I thought this code didn’t work, and had many headaches over it, but I was doing it on a WAMP server install. When I put it live it worked like a charm. Thanks!

  12. Super stuff! Nice and easy to follow. Have given you a credit in my source code

    Many thanks :)

  13. Great work, I just changed my site default avatar, actually It can also help us to speed up WP a bit :D

Share your comments and opinions:

WP Engine WordPress Hosting