Automattic, the company behind WordPress, recently acquired Gravatar. In case you don’t know, gravatars—or “globally recognized avatars”—are images that you can associate with an email address, so a picture of your choice can appear alongside your comments on many others’ blogs.
Gravatar already provides a simple WordPress plugin, but it suffers from a common plugin problem: if you add its “gravatar” function to your comments template, you either have to wrap it in a function_exists check, or you’ll get a fatal error when you deactivate the plugin.
My gravatar plugin lets you use WordPress’s action hook callback system, so it just disappears when the plugin is deactivated.
How it works:
-
If you just want to display a commenter’s gravatar image, just add
<?php do_action('gravatar') ?>
to the appropriate place in your WordPress comments.php template. By default, it will generate a gravatar image.
-
But you can customize it as much as you want, by passing an array of arguments to the ‘gravatar’ action hook. On my blog I’ve done the following:
<?php do_action('gravatar', array('div_class' => 'gravatar', 'return' => 'div', 'default' => get_bloginfo('stylesheet_directory') . '/images/default_gravatar.png', 'rating' => 'PG')) ?>
That tells the plugin to wrap the gravatar image in a
divwith a class of “gravatar”, using a default gravatar image located in my theme’s images directory, and returning only PG-rated gravatars.Here is a complete list of arguments you can pass to the gravatar hook:
alt: Text for thealtattribute of the gravatar image.border,b: An argument you can pass to the gravatar server, the “border” is a hex color to be generated around the gravatar image.comment_author_email: The email on which to base the gravatar. By default, the plugin gets this from the comment’s author email.default,d: The default image to use if no gravatar is available.div_class: A class for thedivelement surrounding the gravatar, if you’ve set “return” to “div.”echo: Whether to echo the result (true by default). If you wanted to assign the gravatar image URL to a variable named$path, you would do the following:
<?php $path = apply_filters('gravatar', array('echo' => false, 'return' => '')); ?>
gravatar_id,g: The gravatar id. Usually this is generated from the commenter’s email, but if you want you can pass it directly.img_class: A class for the gravatarimgelement.rating,r: The rating of allowed gravatar images, as explained at the gravatar website. Optinos are G, PG, R, and X.return: What to return. By default, this is an image element (”img”). You can also select “div” for adivelement around an image, or just return the path to the image with “return” set to “”.pattern: Any string containing%s; the plugin will return that string, replacing%swith the gravatar image path.For example, setting “pattern” to
would return the gravatar image in aspanelement.size,s: The size of the gravatar image, in pixels. Currently it can range between 1 and 80.
Download
Filosofo Gravatar Plugin 1.5 | March 15, 2007
If you have problems, questions, or suggestions, please leave a comment below or open a ticket in my support forum.
See some of the other WordPress plugins I’ve created.
Like this plugin? Is it worth a latte?
This month I have received $44.00 for my plugin coding work, which is about $0.01 per download.

5 Trackbacks/Pingbacks
[...] Filosofo Gravatars [...]
[...] WordPress Plugin from the Gravatars site, or chose from the Easy Gravatars WordPress Plugin or WordPress Gravatar Plugin by Il Filosofo. Both have excellent features and [...]
[...] Filosofo’s Wordpress Gravatar Plugin [...]
[...] Filosofo Gravatars (for displaying a commenter’s gravatar when applicable, although I may soon be switching to something using mybloglog instead.) [...]
[...] Filosofo Gravatars [...]
25 Comments
Great plugin. Thanks for release this.
I’m having the damndest time trying to figure out how to make the gravatars smaller. I’m using a css stye of:
.gravatar { float: right; padding: 4px; margin: 0 0 2px 7px; display: inline; }
When I enter a value of “height: 60 px; width 60 px” nothing seems to change. What am I doing wrong?
Thanks.
Thanks for making it easy to support Gravatars on Wordpress.
The plugin works great. I ended up customizing it using this code.
[php]
< ?php do_action('gravatar', array('div_class' => ‘gravatar’, ‘return’ => ‘div’, ‘default’ => get_bloginfo(’url’) . ‘/images/gravatar.jpg’)) ?>
[/php]
Thanks
Rick, I’d forgotten to include in the description of the API that you can select the size of the gravatar that you want. For example, the following would return gravatars of size 60 pixels:
[php]
< ?php do_action('gravatar', array('size' => 60)) ?>
[/php]
The following doesn’t appear to work for me when reducing the size of the avatar:
60, ‘div_class’ => ‘gravatar’, ‘return’ => ‘div’, ‘default’ => get_bloginfo(’/wp-content/themes/hight-tech-10/hight-tech-10/style.css’) . ‘/images/default_gravatar.png’)) ?>
What part of the code is incorrect?
Also, and I realize this is more of a design question, but how do I make sure the box around my comments accounts for the image and goes around the image too? On some of my shorter comments, the bottom of the avatar hangs below and outside of the box around the comments.
Thanks
It looks like you’re missing the first part of your code. Try pasting it again, this time putting it between <code> tags.
Here is the classic advice on how to clear a float.
Wow thats one pretty cool tweak. I really like the ability to change the size. I’ll try it out on a sports blog.
Great plugin, thank you…
This would be a bit more user friendly if the arrays were configurable options in the form of checkboxes or text fields in the admin panel. If your a non programmer, this code is a bunch of trial and error to get it to work right.
Nice plugin, thanks
I am use it and really happy with it, thanks
Hiya,
Im also using Filosofo Gravatars and after managing to fiddle around with the options its running nicely.
BUT its missing a feature:
Whenever a comment is created by a trackback or pingback, it should not display the gravatar. I dont have comments and track/pingbacks separated and my theme doesn’t seem to support plugins doing so (and I guess there are other ppl with the same problem). I’d really appreciate a plugin update
Will try this at my site later today!
Hmmm..my picture doesn’t appear here either. Do I need your plugin and the gravatar plugin both?
Figured it out. Is this array(’size’ => 60) a second array or is this ’size’ => 60) inserted after a comma after another argument? (Not a coder.) I tried a comma after ‘PG’ and inserted the size argument but caused the page to stop writing before the comment box. This works: ‘gravatar’, ‘return’ => ‘div’, ‘default’ => get_bloginfo(’stylesheet_directory’) . ‘/images/popw.gif’, ‘rating’ => ‘PG’)) ?>
Just not sure where the size code goes.
Great plug in.
The size is part of the rest of the array, so you could do something like this:
[php]
< ?php do_action('gravatar', array('div_class' => ‘gravatar’, ‘return’ => ‘div’, ‘default’ => get_bloginfo(’stylesheet_directory’) . ‘/images/default_gravatar.png’, ‘rating’ => ‘PG’, ’size’ => 60)) ?>
[/php]
I know it’s not incredibly user-friendly, but I really wanted something lightweight and straightforward. The other Gravatar plugins are bloated, in my opinion.
I’ve actually created a Gravatar class if you click the link in my name. Loosely coupled and works like a dream - it also has a cache with an expiration date for the avatar - to save on loading times. It can merely load the avatar in locally.
Adam @ TalkPHP.com
Works perfectly. Thanks!
Now if only they’d create a Wordpress.com Avatar API so we could grab those too.
I’m only able to see the default gravatar icon for some reason.
Could it be because I’m using the Co-Authors plugin?
Thanks much,
Andy
working now — i think it must have been a delay on gravatar’s server. thx.
Great plugin! Now I just have to figure out how to get it working
I cannot get the gravatar images to show up. Do I need something besides this file…
filosofo-gravatars.php
placed here:
/httpdocs/blog/wp-content/plugins/
and this code:
placed in this file…
blog\wp-content\themes\blix\comments.php
I need help with this. Can I pay you?
Oops, the code disappeared.
here it is again…(I forgot to enclose it with the code tags)
[php]
< ?php do_action('gravatar') ?>
[/php]
Is there an easy way to eliminate the default image entirely, leaving only an empty space w/o image tags for commenters not registered with gravatar?
hi. Thanks for plugin
perfect.
regards
See some of the other WordPress plugins I’ve created.
Like this plugin? Is it worth a latte?
This month I have received $44.00 for my plugin coding work, which is about $0.01 per download.
Post a Comment