Filosofo’s WordPress Gravatar Plugin

by filosofo. Posted on October 23, 2007 at 12:50 pm

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 div with 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 alt attribute 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 the div element 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 gravatar img element. 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 a div element 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 %s with the gravatar image path.

For example, setting “pattern” to [image] would return the gravatar image in a span element.

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?

My Amazon.com Wish List

This month I have received $40.00 for my plugin coding work, which is about $0.02 per download.

10 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 [...]

[...] Filosofo Gravatars [...]

[...] 57. Filosofo’s Wordpress Gravatar Plugin The plugin fixed my issue of not being able to get gravatars to work with my blog. If you’re looking to put gravatars on your site in the comment section, Filosofo’s code is well written. Share And Enjoy //Random iframe content- © Dynamic Drive (www.dynamicdrive.com) //For full source code, and Terms Of use, visit http://dynamicdrive.com //This credit MUST stay intact for use var ie=document.all&&navigator.userAgent.indexOf(”Opera”)==-1 var dom=document.getElementById&&navigator.userAgent.indexOf(”Opera”)==-1 //Specify IFRAME display attributes var iframeprops=’width=500 height=225 marginwidth=”0″ marginheight=”0″ hspace=”0″ vspace=”0″ frameborder=”0″ scrolling=”no”‘ //Specify random URLs to display inside iframe var randomcontent=new Array() randomcontent[0]=”http://eventurebiz.com/blog/wp-content/themes/techland-10/random/r_1.htm” randomcontent[1]=”http://eventurebiz.com/blog/wp-content/themes/techland-10/random/r_2.htm” randomcontent[2]=”http://eventurebiz.com/blog/wp-content/themes/techland-10/random/r_3.htm” //No need to edit after here if (ie||dom) document.write(”) function random_iframe(){ if (ie||dom){ var iframeobj=document.getElementById? document.getElementById(”dynstuff”) : document.all.dynstuff iframeobj.src=randomcontent[Math.floor(Math.random()*randomcontent.length)] } } window.onload=random_iframe Related Posts Hack WordPress [...]

[...] Gravatars - 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. [...]

wordpress…

Its just great! There are so many sites, which offer free wordpress themes. And if you are searching for a real unique design, there are thousands of programmers and designers which offer their work. Nice to be part of the great wordpress-community. :-…

[...] Filosofo Gravatars [...]

25 Comments

n-blue commented on October 24, 2007 at 5:18 am | Permalink
n-blue

Great plugin. Thanks for release this.

Rick commented on October 24, 2007 at 4:42 pm | Permalink
Rick

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.

couchmouse commented on October 24, 2007 at 6:08 pm | Permalink
couchmouse

Thanks for making it easy to support Gravatars on Wordpress.
The plugin works great. I ended up customizing it using this code.
< ?php do_action('gravatar', array('div_class' => ‘gravatar’, ‘return’ => ‘div’, ‘default’ => get_bloginfo(’url’) . ‘/images/gravatar.jpg’)) ?>

Thanks

filosofo commented on October 24, 2007 at 10:11 pm | Permalink
filosofo

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 do_action('gravatar', array('size' => 60)) ?>

Rick commented on October 25, 2007 at 9:49 am | Permalink
Rick

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

filosofo commented on October 25, 2007 at 10:57 am | Permalink
filosofo


What part of the code is incorrect?

It looks like you’re missing the first part of your code. Try pasting it again, this time putting it between <code> tags.

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?

Here is the classic advice on how to clear a float.

Dave commented on October 30, 2007 at 12:23 pm | Permalink
Dave

Wow thats one pretty cool tweak. I really like the ability to change the size. I’ll try it out on a sports blog.

AnnaBella commented on November 3, 2007 at 11:32 am | Permalink
AnnaBella

Great plugin, thank you… :)

Jeffro2pt0 commented on November 11, 2007 at 4:46 pm | Permalink
Jeffro2pt0

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.

mygain commented on November 12, 2007 at 5:00 am | Permalink
mygain

Nice plugin, thanks

Jauhari commented on November 12, 2007 at 7:55 am | Permalink
Jauhari

I am use it and really happy with it, thanks

Georg commented on December 12, 2007 at 7:10 pm | Permalink
Georg

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 ;)

M commented on December 18, 2007 at 11:56 am | Permalink
M

Will try this at my site later today!

Doug Smith commented on January 5, 2008 at 5:54 pm | Permalink
Doug Smith

Hmmm..my picture doesn’t appear here either. Do I need your plugin and the gravatar plugin both?

Doug Smith commented on January 5, 2008 at 7:00 pm | Permalink
Doug Smith

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.

filosofo commented on January 5, 2008 at 7:40 pm | Permalink
filosofo

The size is part of the rest of the array, so you could do something like this:

< ?php do_action('gravatar', array('div_class' => ‘gravatar’, ‘return’ => ‘div’, ‘default’ => get_bloginfo(’stylesheet_directory’) . ‘/images/default_gravatar.png’, ‘rating’ => ‘PG’, ’size’ => 60)) ?>

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.

Adam commented on January 8, 2008 at 10:45 pm | Permalink
Adam

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

Jason commented on January 21, 2008 at 8:42 am | Permalink
Jason

Works perfectly. Thanks!

Now if only they’d create a Wordpress.com Avatar API so we could grab those too.

andy commented on February 14, 2008 at 3:44 pm | Permalink
andy

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

andy commented on February 15, 2008 at 8:40 am | Permalink
andy

working now — i think it must have been a delay on gravatar’s server. thx.

Tommy commented on February 27, 2008 at 4:00 pm | Permalink
Tommy

Great plugin! Now I just have to figure out how to get it working :)

Joni Solis commented on March 3, 2008 at 3:42 pm | Permalink
Joni Solis

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?

Joni Solis commented on March 3, 2008 at 3:45 pm | Permalink
Joni Solis

Oops, the code disappeared.
here it is again…(I forgot to enclose it with the code tags)

< ?php do_action('gravatar') ?>

smijer commented on March 12, 2008 at 7:18 pm | Permalink
smijer

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?

baron commented on May 13, 2008 at 12:37 pm | Permalink
baron

hi. Thanks for plugin

perfect.

regards

See some of the other WordPress plugins I’ve created.
Like this plugin? Is it worth a latte?

My Amazon.com Wish List

This month I have received $40.00 for my plugin coding work, which is about $0.02 per download.

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*

Subscribe without commenting.


You are viewing a mobilized version of this site...
View original page here

Mobilized by Mowser Mowser