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.


Posted

in

by

Comments

39 responses to “Add Avatar To WordPress Default”

  1. WPCult Avatar

    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 Avatar

    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 Avatar

    Didn’t work for me either.

  4. […] to WpEngineer for this great hack! Did this tip help you? If so, feel free to make a small […]

  5. Outraged Richard Avatar

    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!

  6. […] eso es todo. Facilito, ¿huh?[1] Gracias a wpenginer.com [↩] Dr. WordPress 25 Enero 2009 Trucos, WordPress Funciones, […]

  7. Michael Avatar

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

    Rober, it works fine for me.

  8. Outraged Richard Avatar

    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!

  9. Alex Avatar

    @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.

  10. Leon Poole Avatar

    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 🙂

  11. Alex Avatar

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

  12. Steven Avatar

    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.

  13. Remkus Avatar

    @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?

  14. rodrigo Avatar
    rodrigo

    thanks!

  15. Jeremiah Avatar

    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!

  16. […] Source – (I’ve customised it a tad, originally from) WPEngineer. […]

  17. T-Law Avatar

    That’s great, simple and useful, thanks.

  18. T-Law Avatar

    It works, thanks.

  19. suavi Avatar
    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.

  20. Danny Avatar

    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.

  21. Frank Avatar

    @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.

  22. 摇滚帮 ROCK BUNCH Avatar

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

  23. Fabian Avatar

    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.

  24. eavasi Avatar

    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/

  25. […] original: Add Avatar To WordPress Default 15 de Septiembre de 2009 Etiquetas: avatar, WordPress Canal RSS para los comentarios de esta […]

  26. Wardell Avatar

    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

  27. Najee Khan Avatar

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

  28. ezhil Avatar

    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.

  29. Chris Avatar

    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?

  30. Eric Avatar

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

  31. Franklin Manuel Avatar

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

  32. […] Hol dir den Codeschnipsel für dieses Feature auf der WPEngineer-Webseite. […]

  33. Leyton Jay Avatar

    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!

  34. Greg Habermann Avatar

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