How to Change the Default Gravatar in Wordpress

Change your GravatarAs 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:

Meet the Mystery Man

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 Selection


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:

  1. The add_filter function says that we’ll be editing the avatar_defaults array using our custom function “newgravatar
  2. The newgravatar function is responsible for providing the location and title of our additional gravatar option
  3. The first line in the function points to the new gravatar in the images folder of the template directory
  4. The second line designates the name that will be displayed next to the image dashboard options.
  5. 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.

  • Stumble It!
  • Bookmark It!
  • Tweet it!

About Zach Dunn

Zach is a partner and interface designer at One Mighty Roar from Massachusetts, USA. Follow him on Twitter @zachdunn.

 

Discussion

  1. Andrew Houle

    February 19th, 2009 at 7:46 PM

    Nice tip Zach! I will definitely be customizing this in my redesign. Bookmarked!

  2. Zach Dunn

    February 19th, 2009 at 8:34 PM

    @Andrew

    Haha, you’d think I would have noticed you’re still representing the mystery man community by now! In any case, glad to be of help!

  3. joyoge designers' bookmark

    February 20th, 2009 at 2:57 PM

    nice tip thanks a lot..

  4. whatever

    February 21st, 2009 at 7:21 PM

    Nice to see this tip spread across the net ;-) I discovered this here a month ago and it’s still working great. HowTo: Set A Custom Gravatar Image In Wordpress 2.7

    This is also very handy for theme developers to raise usability of their theme. So nice you mention it here. Would love to see this more in use.

  5. Amanda

    March 6th, 2009 at 2:04 AM

    Dude, you rock! Thanks so much, exactly what I was looking for. :) See it live on my blog. :)

    P.S. I *much* prefer the clean white background of your site. ;)

  6. Sander

    March 8th, 2009 at 2:52 PM

    Sorry, off-topic question, but what is the font used in the picture? I really like it!

  7. Zach Dunn

    March 8th, 2009 at 3:32 PM

    @Sander

    That is Archer semibold. Of of my personal favorites. Glad you like it too!

    http://www.typography.com/fonts/font_styles.php?productLineID=100033

  8. chibi

    April 18th, 2009 at 7:42 PM

    Thank you so much :) Just what I needed!

    chibi’s last blog post..#4 Moon Crisis Make-Up!

  9. Ydwer

    April 21st, 2009 at 2:53 AM

    Hi, I tried to implement the code above in my functions.php. But after uploading it on my ftp I get a blank screen on my wp-admin.

    What am I doing wrong?

    Ydwer’s last blog post..Dit is een testbericht

  10. Ydwer

    April 21st, 2009 at 2:59 AM

    I just figured it out. I added some new around the code and that cause the problem.

    Cheers, great code!

    Ydwer’s last blog post..Dit is een testbericht

  11. Luc Latulippe

    April 29th, 2009 at 7:26 PM

    Sweet! Thanks!

  12. Frank

    June 29th, 2009 at 2:10 PM

    Thanks, works perfectly.
    .-= Frank´s last blog ..140Ware opens up Alpha Invites for Tweetboard =-.

  13. themeheven

    July 20th, 2009 at 11:35 PM

    Nice works Thanks!
    .-= themeheven´s last blog ..Optimizing PNG for the web =-.

  14. m7zhao

    July 22nd, 2009 at 3:33 PM

    Awesome, just what I was looking for! Thanks!

  15. Mike

    August 14th, 2009 at 4:54 PM

    Great it works !

    Greetz from a happy belgium guy who isn’t a mystery anymore ;)

  16. oron mizrachi

    August 15th, 2009 at 7:57 AM

    very cool and informative tutorial!! 10x

  17. clickfire

    August 21st, 2009 at 11:13 PM

    Very easy and worked perfect for me, thanks.
    .-= clickfire´s last blog ..Twitter Spam Attack =-.

  18. subcorpus

    September 8th, 2009 at 12:58 PM

    thanks for the tip … appreciated …
    .-= subcorpus´s last blog ..The big cat roars in … =-.

  19. Harsh Agrawal

    September 13th, 2009 at 5:29 PM

    Cool ..Thanks..I always change the default icon to something fancy, though adding one of your own is great advice. :)
    .-= Harsh Agrawal´s last blog ..Difference Between Free Wordpress and Self Hosted Wordpress Blog =-.

  20. Jason Saggers

    September 16th, 2009 at 6:16 AM

    Thanks for this, it works perfectly.
    Now if only I was good at creating a custom avatar.
    Great site by the way.

  21. Alex.

    October 2nd, 2009 at 6:45 PM

    I made the change as suggested. But it change the gravatar for all users. How do I change single user profile. In the user profile there is no gravatar selection of custom name field.
    Thanks in advance

  22. bob smith

    October 7th, 2009 at 12:16 PM

    Doesn’t work for Wordpress 2.8.4….there is no such code in the functions.php anymore – so now what?

  23. Zach Dunn

    October 7th, 2009 at 1:09 PM

    @bob

    The code in this tutorial was never available by default on any Wordpress version. As evidence by this blog, you can get it working in Wordpress 2.8.4, but you’re going to have to put the code into the functions.php file (or create one from scratch).

  24. Cenk

    October 15th, 2009 at 6:26 AM

    Check:

    http://codex.wordpress.org/Using_Gravatars#How_a_Gravatar_is_Constructed

    &default=http%3A%2F%2Fwww.somesite.com%2Fsomeimage.jpg

    So, it can be done right into wordpress now :-)

  25. ezhil

    December 17th, 2009 at 12:58 AM

    simple and easy way to bring gravatar in your comments http://flexlearner.wordpress.com/2009/12/17/how-to-add-gravatar-to-your-wordpress-comments/ it just took me 5 minutes to do that.

  26. zplits | what's the latest?

    December 29th, 2009 at 4:16 AM

    Hi there, i’m having problems with the steps that you provide. I have followed it, but when i view it in mozilla firefox it doesn’t show up, but in google chrome and ie it works.

    Am i missing something?

  27. Rednights

    January 4th, 2010 at 6:03 PM

    Worked like a charm! Thanks for the code. Now only if pushing the footer all the way to the bottom codes worked just as easily =(

  28. deegee

    January 6th, 2010 at 7:15 AM

    Awesome content, im a newbie for a wp blogger thx 4 the tutorial

  29. Ewan

    January 7th, 2010 at 4:17 PM

    Just what the mad doctor ordered!

    Thx,
    E

  30. Kevin Oh!

    January 8th, 2010 at 11:49 PM

    Perf. Worked absolutely perfectly.

    Thank you!

  31. lawrence77

    January 9th, 2010 at 4:10 PM

    wow, thanks…

  32. abhi

    January 25th, 2010 at 8:48 AM

    Thanks for this. It solved my problem. Cheers :)

  33. Sam

    February 3rd, 2010 at 10:29 AM

    Thanks for this tip. Haven’t done it yet, but it seems simple enough.

    Here’s another question. Hope you do not mind.

    How do I get my gravatar to default to the left side of my comments instead of the right? Just looks so much better and appropriate on the left.

    Thanks

  34. Zach Dunn

    February 3rd, 2010 at 12:09 PM

    @Sam

    Just use some CSS to float the image to the left. That should do it just fine!

  35. sushilshirbhate

    February 25th, 2010 at 12:04 AM

    nice tip thanks a lot..

  36. Patience

    March 5th, 2010 at 2:09 PM

    Thanks worked great!

  37. Cold Crush

    March 16th, 2010 at 3:01 AM

    Hi, it didn’t quite work for me — it still seems to be querying the gravatar server, instead of my custom icon it shows a broken link style image with a question mark, and the url instead of being from my own blog is showing up as a gravatar URL with a whole bunch of garbled junk after

  38. Cold Crush

    March 16th, 2010 at 3:03 AM

    instead of

  39. Cold Crush

    March 16th, 2010 at 3:41 AM

    Ok nevermind I worked it out haha. Thanks for the tutorial!

Join the Conversation!

Remember: Life's not all doom and gloom, so please keep it constructive. If we've made an error or missed something big, please let us know! Learning is revisions, after all.

CommentLuv is Enabled

 

Sponsors

Advertise on Build Internet!