Several commenters mentioned on yesterday’s post regarding Gravatars without a plugin that WordPress 2.5 would be having built-in Gravatar support.
And indeed WordPress 2.5 will come with Gravatar (aka, Avatar) support. Within this post I will demonstrate how Gravatars will be used with WordPress 2.5. As a side note, 2.5 has yet to be released as of this writing.
Gravatars in the WordPress Admin Panel
WordPress 2.5 marries theme authors and casual WordPress users together with support for Gravatars in the WordPress admin panel.
WordPress users can access the Gravatars settings in the Settings->Discussion panel.
If a theme author has decided to use the WordPress 2.5 function, then WordPress users can easily control their Gravatar usage in the admin panel.
In the admin panel, WordPress users can change:
- Whether Avatars (aka, Gravatars) are displayed or not.
- Which rating of Avatars are shown.
Avatars in the WP Admin Panel
Please keep in mind these settings will have no effect if the theme author didn’t make use of the WordPress 2.5 function.
And what is the functon you ask?
Theme Authors: Adding Gravatars to Your Theme
The function to add Gravatars to your theme is called: get_avatar. The function returns a complete <img> tag of the Avatar.
The function get_avatar is setup as follows:
function get_avatar( $id_or_email, $size = '64', $default = '' )
- id_or_email: The author’s User ID (an integer or string) or an E-mail Address (a string)
- size: The size of the Avatar to display (max is 80).
- default: The absolute location of the default Avatar.
Some things to note here:
- The default Avatar size is 64×64.
- The default Avatar is
- The Avatars will show only if the user allows them in the WP Admin Panel (enabled by default).
- The Avatars will only show based on the rating the user has selected in the WordPress admin panel.
Here are some examples of the function in use:
Example 1 with a default Avatar
echo get_avatar( 1, '80', 'http://mysite.com/avatar/avatar.gif' )
In this example I used a user ID of ‘1‘ and specified the location of a default location. This example is useful if you want to display an Avatar outside of the comment’s section and you have an idea of the person’s User ID.
Example 2 with an e-mail address and size
echo get_avatar( 'myname@mysite.com', '60' )
This example is useful if you want to display the Avatars within the Comments Loop.
If calling Avatars from the Comments Loop (in comments.php), you will want to use the get_comment_author_email function in place of the above e-mail address.
echo get_avatar( get_comment_author_email(), '80' )
Backwards Compatibility
If you wish to develop a theme that displays Avatars for 2.5 and below, I recommend using a combination of the code mentioned here and the code from Connor Wilson’s post on using Gravatar without a plugin.
Your code would look something along the lines of:
if (function_exists('get_avatar')) { //2.5 code } else { //alternate gravatar code for < 2.5 }
Further Reading
For more reading on the get_avatar function, please read Ryan Boren’s Avatars in WordPress 2.5, which he covers several additional points not mentioned here.
Conclusion
The inclusion of the get_avatar function is a nice addition to WordPress 2.5, but it relies on theme authors to include it. If you are comfortable editing your own theme, you can easily add the function into your own theme when 2.5 is released.
Thanks Ronald. I need this to prepare Wpdesigner.com’s themes for 2.5.
@Small Potato,
You’re welcome. This post was written for theme developers like you. Hope all goes well on your front.
Will the 2.5 function automatically hash the lowercase version of the email address? This seems to be a persistent issue, with users leaving mixed-case version of their email address with their comments.
Michael,
It does do a lowercase version from what I can see.
Gravatars are part of the WP Core now? I know Automattic is running them now, but why isn’t this feature a plugin like Akismet? Seems counter to other running philosophies. :/
I don’t think it is. I mean, gravatar has been bought by WordPress, but to display it, you have to use code or so.
@Jeff – I reckon though that because you can turn it off, it is like using a plugin, you aren’t forced to use it, but so many people do that I guess it was deemed useful to have it in the core.
I’ve personally never used them, but who knows, maybe this will be what I need to start!
Wow, this would be great! Thanks…
As I understand it, they were added into WP core not merely because this is an oft-requested feature, but also because theme and plugin authors can override the function in order to pull in avatars from sources other than Gravatar.
In other words, while it defaults to using Gravatar, it’s more generic than that: it’s a WordPress avatar API, not a way of tying all WordPress users and developers into a proprietary service.
@Benedict,
Yes, the function is pluggable, so it can be used with other services. Thanks for bringing that up.
Nice.
Also i liked to be able to show a gravatar outside the loop.
Nice tips.-
Silla!
So, I am confused.. So, is the Gravatar system in WP 2.5 similar to that of http://site.gravatar.com/ or is it going to be a completely new rewrite?
reason I’m asking, is because I can’t see WP users wanting to upload an avatar to every blog they visit, and since the gravaar.com method has been a little “flaky” as of late, is wordpress planning on integrating it into the wordpress.com API? or with gravatar.com, or both or what? I am confused as to what this means
in either case, I think this is a smart move, because the gravatar.com service doesn’t work 90% of the time, or the gravatar plugins they do have for wordpress don’t work 90% of the time. It drives me nutts, because my gravatar NEVER works on anyone elses site but mine, so I take it up with the plugin authors and they blame gravatar.com, so I take it up with gravatar.com and they say its the plugin authors fault. arrgh. its frustrating, so however WP plans on doing it, is a very very good thing, FINALLY gravatars may actually WORK in wordpress thank the good lord. hehe
This sounds very exciting. You all are doing a great job. I can’t wait for 2.5!
Ronald,
Perfect timing for posting this – I hadn’t heard they were adding gravatar support in 2.5, so I had made my own function. But now I can cut my plugin size and use the Avatar API.
Thanks for the heads up!
@bubazoo,
Gravatars, Avatars, whatever you’d like to call them, are using the Gravatars site. The main thing is that the core code now contains the function to retrieve the Gravatar rather than relying on a third-party plugin.
I imagine a lot of the times the Gravatars don’t work because of case issues, trim issues, or perhaps in my case I use a variant of my original e-mail address. Gravatars have improved significantly since Automattic took over them.
@Jason,
You’re function will still be useful for blogs < 2.5.
Can’t wait to see WP 2.5!
@Ronald
Yep, I re-read your post after I made that comment, and saw that part in there – Thanks!
I’m using the “gravatars with a plugin” code on my website, it almost works.
I am registered with grvatar and my gravatar doesn’t show, I’m looking forward to trying the functionality in 2.5.
http://blog.yourwish.co.nz/200.....dding-day/
I’ve tried the function, cant make it work, the encoded email is not md5 as md5(“email”) dont know why looking for the function now
good feature but we are expecting more from wordpress next stable releases…..
User error, its all working perfectly, though I am looking forward to trying out the built in gravatars in 2.5.
Just found this article, thanks for the heads up! Gotta update my theme and test it with RC1
This is very, very nice =)
I want to add a little update here. I searched a lot but couldn’t find the gravatar option in WordPress 2.5. After some more searching, I found it isn’t on the reading panel, but it’s on the discussion panel. Maybe you can edit the post and update this.
Thanx fot the the great blog you’re running, it’s my number one source for wordpress 🙂
“WordPress users can access the Gravatars settings in the Settings->Reading panel”
I find this misleading… the Gravatars setting is in the SETTINGS -> Discussion panel (scroll to the bottom) instead.
Thanks guys. Post has been updated. When I wrote it, it was in the Reading panel. As WP 2.5 was released, it was moved to Discussion.
Well I got it working. Now I just need to get it working with Comvatars. It looks like the upgrade broke that plugin.
Gooed piece of information, I had to find it out own my own to made it work on my blog here.
I finally made use of a plugin 😉
What is the difference between Gravatars and Avatars, I mean the key factor. No one can answer me.
@Web Design Manager,
Gravatars are the same as Avatars. There is no difference.
Gravatars stand for Globally Recognized Avatars. GR-Avatars.
Any system that utilizes the Gravatars API will be able to pick yours up from your e-mail address.
??????????? CJ?? http://www.8cj.cn
As I understand it, they were added into WP core not merely because this is an oft-requested feature, but also because theme and plugin authors can override the function in order to pull in avatars from sources other than Gravatar.
Doesn’t work on my theme. 🙁
Thanks for the helpful info
my avatars don’t work on my own website either… i tried using the option here: http://www.dezzain.com/wordpre.....tegration/ but all it did was mess up my comments portion…. any idea?
Excelent tuto , iwas looking exactlly for this 🙂
Has anyone had any problems with the avatar-default class being assigned to the image if the default image is used. For some reason that class isn’t being assigned when the default avatar is used. I’m trying to hide the avatar if the default is used and have no way to differentiate the default images from the rest.
Gravatars are a great adition to WordPress.
Thanks for the article.. I didn’t realize even though gravatar’s are built into wordpress that you still have to install the gravatar plugin. Got it working now.. thanks.
Nice post. I have been searching for this exact info. Thanks!
Doesn’t seem to work…..
Hi, how can i get 2.5 themes?
I was getting the error “Unexpected character input”, but I was able to fix it using double quotes around the number value for size.
Ca we make our own gravatar image on wordpress.com?
After a long overdue upgrade, the tips worked like a charm. Thanks!