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.
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
39 responses to “Add Avatar To WordPress Default”
[…] Add Avatar To WordPress Default […]
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.
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.
Didn’t work for me either.
[…] custom “default” gravatar […]
[…] to WpEngineer for this great hack! Did this tip help you? If so, feel free to make a small […]
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!
[…] eso es todo. Facilito, ¿huh?[1] Gracias a wpenginer.com [↩] Dr. WordPress 25 Enero 2009 Trucos, WordPress Funciones, […]
Sajid, it looks like your imagename or file extension is wrong.
Rober, it works fine for me.
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!
@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.
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 🙂
Hey Leon, as of I understood what you like to achieve, you should check out comment #10, hope that helps.
[…] Add Avatar To WordPress Default […]
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.
@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?
thanks!
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!
[…] Source – (I’ve customised it a tad, originally from) WPEngineer. […]
That’s great, simple and useful, thanks.
It works, thanks.
[…] 2. Znalezione na: WPengineer.com. […]
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.
I would like to suggest this small change to the code:
In this way, it works also with child themes.
@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.
How about make this into a plugin? that will be super awesome..
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.
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/
[…] original: Add Avatar To WordPress Default 15 de Septiembre de 2009 Etiquetas: avatar, WordPress Canal RSS para los comentarios de esta […]
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
After the fix Remkus suggested, mine works too. Thank You.
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.
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?
[…] to WpEngineer for this such great wordpress […]
Thanks. Very Very helpful. Got this to work on my site. Check it out!
You can also check out my post on “how to change default gravatar in WordPress” here http://goo.gl/fb/t7Qey
[…] Hol dir den Codeschnipsel für dieses Feature auf der WPEngineer-Webseite. […]
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!
Used this trick today in WP 3.1.1 for a client and worked like a charm. Thanks mate!