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 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 $44.00 for my plugin coding work, which is about $0.01 per download.

5 Trackbacks/Pingbacks

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

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

  3. [...] Filosofo’s Wordpress Gravatar Plugin [...]

  4. [...] Filosofo Gravatars (for displaying a commenter’s gravatar when applicable, although I may soon be switching to something using mybloglog instead.) [...]

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

25 Comments

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

    Great plugin. Thanks for release this.

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

  3. 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]
    < ?php do_action('gravatar', array('div_class' => ‘gravatar’, ‘return’ => ‘div’, ‘default’ => get_bloginfo(’url’) . ‘/images/gravatar.jpg’)) ?>
    [/php]

    Thanks

  4. 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]
    < ?php do_action('gravatar', array('size' => 60)) ?>
    [/php]

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

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

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

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

    Great plugin, thank you… :)

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

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

    Nice plugin, thanks

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

    I am use it and really happy with it, thanks

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

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

    Will try this at my site later today!

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

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

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

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

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

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

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

  21. 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 :)

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

  23. 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]
    < ?php do_action('gravatar') ?>
    [/php]

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

  25. 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 $44.00 for my plugin coding work, which is about $0.01 per download.

Post a Comment

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

Subscribe without commenting.

Comment moderation is enabled. Your comment may take some time to appear.