Add Avatar To WordPress Default

Avatars are popular as identification and with the service Gravatar it is largely used in the comment area of different applications.
WordPress allows the user some basic settings. For example loading a default or generated Avatar, if the commentator doesn't have one.
Various themes have a standard Avatar to match the design. But even here there is a nice solution via a hook, which enables to add an avatar in the list of your backend to make a selection quite simple. In the following I would like to show with a short and simple code snippet how to add 2 new avatars to the array of WordPress.

addavatar

You have to add this little function to functions.php of your theme and it will add 2 Avatar from your folder images of your theme directory. I suggest, that the Avatars have a size of 60 pixel.

/**
 * add a default-gravatar to options
 */
if ( !function_exists('fb_addgravatar') ) {
	function fb_addgravatar( $avatar_defaults ) {
		$myavatar = get_bloginfo('template_directory') . '/images/avatar.gif';
		$avatar_defaults[$myavatar] = 'people';

		$myavatar2 = get_bloginfo('template_directory') . '/images/myavatar.png';
		$avatar_defaults[$myavatar2] = 'wpengineer.com';

		return $avatar_defaults;
	}

	add_filter( 'avatar_defaults', 'fb_addgravatar' );
}

Is the theme active, you will have the 2 new Avatars available in your list.

Comments are closed.

39 comments

  1. WPCult

    That is a very neat trick, was wondering how I could get around the basic mystery man once I started using the get_avatar to pull the default image.

  2. Sajid Iqbal

    It didn't work for me. Avatars are create as I can see there names but there are no Avatar Images despite I put those in theme's images folder.

  3. Rober

    Didn't work for me either.

  4. Outraged Richard

    Brilliant! It worked with my theme on WP 2.7.

    I am using a picture of a peasant to represent the default avatar.

    My only wish is that the default avatar would be randomly selected among a group of custom avatars. So on my blog, there would be different peasant avatars along the side of the comments.

    It must be a simple bit of code to randomize the custom avatars. I am no coder.

    Hmm... I could do a gif with changing pictures.

    That would be hilarious!

  5. Michael

    Sajid, it looks like your imagename or file extension is wrong.

    Rober, it works fine for me.

  6. Outraged Richard

    I decided I wanted more control and variation over the default avatars, so I am using the Ravatar WordPress plugin.

    Basically, the plugin chooses within your picture folder a random part of an image or the whole image if you only use avatar sizes.

    Instead of having one picture of a peasant I can have dozens, or thousands if I wanted to, of random medieval themed default avatars.

    The neat thing is that the random avatar links up with the person's email address so they can use their given avatar for future commenting!

  7. Alex

    @Outraged Richard, that sounds cool, I guess the Ravatar WordPress plugin is the perfect tool if you want functions like this. Funny, that two different Avatars are showing now.

  8. Leon Poole

    Thanks guys! This tip worked great for me.

    Question: I would like to have one custom avatar for general users (above method solves this) but also a different custom avatar for site admins. Any tips on how this could be done?

    Cheers 🙂

  9. Alex

    Hey Leon, as of I understood what you like to achieve, you should check out comment #10, hope that helps.

  10. Steven

    Hey

    It didn't work for me either, I saw the code everywhere in the top...

    Anyway in the wp-includes there's blank.gif I just placed my avatar there. For one custom avatar: that works.

    Still thanks I'm sure we over saw something.

  11. Remkus

    @Steven: I had the same problem and my way to fix that is to add the <?php before the first if statement and add ?> at the end. Both best be a seperate lines.

    This makes the code above function as a proper php function. I'm not sure why it doesn't work as is though.. can you answer that Frank?

  12. rodrigo

    thanks!

  13. Jeremiah

    I'm using Thesis so I just added this to my custom_functions.php file and dumped the image in a root folder of my blog and it works like a champ. Brilliant! Thanks for the code!

  14. T-Law

    That's great, simple and useful, thanks.

  15. T-Law

    It works, thanks.

  16. suavi

    It works. Thank you so much!

    But be carefull for creating a folder under your theme.

    /wp-content/themes/your_theme_name/images

    Put gif files into this images folder.

  17. Danny

    I would like to suggest this small change to the code:

    /**
    * add a default-gravatar to options
    */
    if ( !function_exists('fb_addgravatar') ) {
    function fb_addgravatar( $avatar_defaults ) {
    $myavatar = get_bloginfo('stylesheet_directory') . '/images/avatar.gif';
    $avatar_defaults[$myavatar] = 'people';
    
    $myavatar2 = get_bloginfo('stylesheet_directory') . '/images/myavatar.png';
    $avatar_defaults[$myavatar2] = 'wpengineer.com';
    
    return $avatar_defaults;
    }
    
    add_filter( 'avatar_defaults', 'fb_addgravatar' );
    }
    

    In this way, it works also with child themes.

  18. Frank

    @Danny: yes, this is possible, have a the problem, when you have your stylesheet in a subdirectory, then must be the images also in this subdirectory of the stylesheet.

  19. 摇滚帮 ROCK BUNCH

    How about make this into a plugin? that will be super awesome..

  20. Fabian

    Just for those that haven't make this tutorial to work!
    Don't forget to add the open and close PHP keys before and after the code.

    Example:

    Otherwise it will not work.

  21. eavasi

    Cool plugin which appealing a new readers to the blog!!!

    Here is descriptions how to add avatar to Show Top Commentatr Plugin http://www.eavasi.ru/show-top-commentators-avatar/

  22. Wardell

    Found the reason why this wasn't working for me, and most likely the others it wasn't working for.

    In the wp-settings.php plugins.php needs to be required before functions.php because plugins.php contains the add filter function

    http://wordpress.org/support/topic/102858

  23. Najee Khan

    After the fix Remkus suggested, mine works too. Thank You.

  24. ezhil

    try this post
    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.

  25. Chris

    I'm trying to get gravatars to work on my site right now. I can't figure out how to change the default editors gravatar. If someone posts a comment and I reply, I want my gravatar to show up with my response.

    Does anyone know how to do this?

  26. Eric

    Thanks. Very Very helpful. Got this to work on my site. Check it out!

  27. Franklin Manuel

    You can also check out my post on "how to change default gravatar in WordPress" here http://goo.gl/fb/t7Qey

  28. Leyton Jay

    That worked perfectly and easily in WP 3.0, thank you.

    I was trying in vain to decipher how the avatars were created and where the markup/data was, wish I'd found this site sooner!

  29. Greg Habermann

    Used this trick today in WP 3.1.1 for a client and worked like a charm. Thanks mate!

10 pingbacks

  1. WPStart.org » Blog Archive » WordPress links week 3 - 2009
  2. CoffeeBear.net » Blog Archive » Updating VectorLover Theme
  3. How to: Add new Gravatars to WordPress default gravatar list
  4. Añadir un Avatar a los que WordPress da por defecto | Dr. WordPress
  5. Geek Week - CSS, jQuery,Wordpress, PHP, XHTML, Web Design, web 2.0 | JasonCypret.com
  6. 10 tricks to make your WordPress theme stand out
  7. WP jak to zrobić - własny awatar | Dzienna dawka kofeiny
  8.   Añadir avatares predeterminados a WordPress | samuelaguilera.com
  9. Add custom gravatars to your wordPress default gravatar list | wordpressapi.com
  10. So optimierst du den Kommentarbereich deines WordPress-Blogs | Elmastudio