Following on from my previous post about doing things the Elgg way, I thought I’d illustrate some of what I was talking about by building out a quick plugin while the kettle boiled for my tea.
This plugin uses the hooks present in Elgg – specifically the getIcon() api – to provide Gravatar icons for users which have not provided their own.
On many platforms this might have been an onerous task, but on Elgg it literally took me 15 minutes (in fact, it took me longer to write this post than the plugin).
If a user defines their own icon it will use that, but if they haven’t provided one it will use their email address to find their gravatar icon.
This is accomplished by setting the priority of the hook somewhere after the icon handler provided by the profile plugin, and before the default icon handler.
Anyway, here is the code:
* Simple gravatar integration for Elgg.
* Scratching an itch! (+ a good example of icon overloading)
* 1) Fallback to elgg default icons instead of gravatar one for missing images
* 2) Have sizes better handle changes in defaults for theming
* @package ElggGravatar
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
* @author Curverider Ltd
* @copyright Curverider Ltd 2008
* @link http://elgg.com/
// Now override icons. Note priority: This sits somewhere between the profile user icons and default icons -
// so if you specify an icon for a user it will use that, else it will try a gravatar icon.
register_plugin_hook('entity:icon:url', 'user', 'gravatar_usericon_hook', 900);
* This hooks into the getIcon API and returns a gravatar icon where possible
* @param unknown_type $hook
* @param unknown_type $entity_type
* @param unknown_type $returnvalue
* @param unknown_type $params
* @return unknown
function gravatar_usericon_hook($hook, $entity_type, $returnvalue, $params)
// Size lookup. TODO: Do this better to allow for changed themes.
$size_lookup = array(
'master' => '200',
'large' => '200',
'topbar' => '16',
'tiny' => '25',
'small' => '40',
'medium' => '100'
if ((!$returnvalue) && ($hook == 'entity:icon:url') && ($params['entity'] instanceof ElggUser))
$size = 40;
$size = $size_lookup[$params['size']];
return "http://www.gravatar.com/avatar/".md5($params['entity']->email) . ".jpg?s=$size";
// Initialise plugin