Filosofo’s WordPress Gravatar Plugin

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 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 $31.50 in donations for the free plugins I offer here, which is about $0.01 per download.

33 Comments

  1. Great plugin. Thanks for release this.

  2. 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.

  3. 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

  4. 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)) ?>

  5. 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


  6. 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.

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

  8. Great plugin, thank you… :)

  9. 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.

  10. Nice plugin, thanks

  11. I am use it and really happy with it, thanks

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

  13. Will try this at my site later today!

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

  15. 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.

  16. 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.

  17. 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

  18. Works perfectly. Thanks!

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

  19. 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

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

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

  22. 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?

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

    < ?php do_action('gravatar') ?>

  24. 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?

  25. hi. Thanks for plugin

    perfect.

    regards

  26. Hi Filosofo,
    your plugin sounds great but I can’t make it work. I placed the simple code on different places in my comments loop but it never shows anything up. Could you please help me out and tell me where I should place it ?
    Thanks !

  27. Hem, ok I’ve found : I hadn’t activated the plugin…

  28. HI,

    i want to replace the default image, if someone has no gravatar (the grey guy), with my own image.

    Is that possible with your plugin or do you have a workaround for it? I use WP 2.7.1 btw :)

  29. @Tari: You can set the default image with the “default” argument (see above).

    Currently I have “default” set to “identicon,” but in the past I’ve used something like the following line:

    <?php do_action('gravatar', array('default' => get_bloginfo('stylesheet_directory') . '/images/default_gravatar.png')) ?>

  30. The plugin works great.

    However, I’d like to position the gravatar to the left of my comments. But I don’t have a clue about how to do this.

    Can you please help?

    -jens

  31. Hi. Not sure exactly where to add the php do_action(‘gravatar’) ?

    The loop code looks like this:

    foreach ($comments as $comment) {
    $output .= “\n
    “.”ID) .
    “#comment-” . $comment->comment_ID . “\” title=\”on ” .
    $comment->post_title . “\”>” .strip_tags($comment->comment_author)
    .”: ” . strip_tags($comment->com_excerpt)
    .”…

    Any ideas?

    Thanks,
    David

  32. Sounds like a great plugin, but I cannot make it work.
    I’ve activated the plugin, uploaded the image under the theme imaages folder and added this in my comments.php editor:

    ‘gravatar’, ‘return’ => ‘div’, ‘default’ => get_bloginfo(‘url’) . ‘/images/default_gravatar.gif’)) ?>

    I’ve also tried:

    ‘gravatar’, ‘return’ => ‘div’, ‘default’ => get_bloginfo(‘stylesheet_directory’) . ‘/images/default_gravatar.gif’)) ?>

    but unfortunately these codes are for me chinese… could u pls tell me what I’m missing?

    Thanks!

  33. Aljaz, take a closer look at the example code: you’re missing array( in a key place.

Post a Comment

Your email is never shared. Required fields are marked *

*
*

22 Trackbacks

  • […] 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 […]

  • Comment Avatar Plugins | AbleReach on January 8, 2008 at 5:30 am

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

  • [BLOCKED BY STBV] wordpress on October 2, 2008 at 12:15 pm

    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 […]

  • […] 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. […]

  • […] Filosofo’s WordPress Gravatar Plugin – Gravatar already provides a simple WordPress plugin, but it suffers from a common plugin problem: […]

  • […] Gravatars,Plugin Homepage » […]

  • […] 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. […]

  • […] 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. […]

  • […] 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 […]

  • got my thumbs up. on March 13, 2009 at 2:05 pm

    got my thumbs up….

    Works a treat….

  • […] In: WordPress plugins 8 Jun 2009 Go to Source […]

  • […] and appends it once they submit. Now this plugin has been used almost in every single blog. 51. Filosofo’s WordPress Gravatar Plugin – Gravatar already provides a simple WordPress plugin, but it suffers from a common plugin […]

  • […] original here: Filosofo’s WordPress Gravatar Plugin – Il Filosofo Comments0 Leave a Reply Click here to cancel […]

  • […] potrzebne do WordPress: Filosofo’s WordPress Gravatar Plugin Comments RSS […]

  • WordPress Plugins Part 1 « MoeMir on April 25, 2010 at 10:14 pm

    […] and appends it once they submit. Now this plugin has been used almost in every single blog. 51. Filosofo’s WordPress Gravatar Plugin – Gravatar already provides a simple WordPress plugin, but it suffers from a common plugin […]