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 the
altattribute of the gravatar image.
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.
d: The default image to use if no gravatar is available.
div_class: A class for the
divelement 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' => '')); ?>
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 gravatar
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 a
divelement 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 a
s: The size of the gravatar image, in pixels. Currently it can range between 1 and 80.
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 $31.50 in donations for the free plugins I offer here, which is about $0.01 per download.