Another itch scratched, I would like to introduce a really simple Open Graph plugin for WordPress.

This plugin adds open graph headers to posts and pages on a per post basis.

It has no native interface, instead it listens out for og:* headers as input in the Custom Fields section of WordPress’ edit page and adds the ones it recognises to the page header as meta tags.

Once installed you will be able to add the following tags to posts:

  • og:title (defaults to page title)
  • og:type (defaults to “article”)
  • og:image (URL, no default)
  • og:url is also used, but this is automatically filled in

I wrote this in about an hour so it isn’t all that fancy, and was designed to solve a specific problem for me. Hopefully it’ll be useful to someone else out there.

Feel free to leave comments and send in any patches!

» Plugin page on Github

Trac_Logo_512x512There are a number of ways that an Elgg plugin developer can manipulate views via the powerful Elgg views system.

Most Elgg programmers are by now familiar with extending or replacing existing views, or providing new views for new objects etc.

But what if you just wanted to make a simple tweak to an existing view – for example to replace all instances of rude words in a feed article, or to turn passive links into active ones?

Well, fortunately Elgg provides a post processing hook for views which can do just that.

After every view has been generated, the framework will trigger a plugin hook called “‘display’, ‘view'”. This hook is passed a parameter ‘view’ which contains the name of the view being processed (eg. object/blog).

The contents of the view are passed in the $returnvalue variable which you can perform any processing on before returning it from the hook.

I have just uploaded a Trac tags plugin to the Elgg community site which provides a good example of this.

The Trac tags plugin is a tiny plugin which uses the views post processing hook to turn Trac links (e.g. #xxxxx for tickets and [xxxxx] for changesets) into active links into your repository, and here’s how – first we register the hook:

function tractags_init()
{
....
// Register our post processing hook
register_plugin_hook('display', 'view', 'tractags_rewrite');


// define views we want to rewrite codes on (means we don't have to process *everything*)
$CONFIG->tractags_views = array(
'object/thewire',
'object/blog'
);

....
}

Then in our handler looks something like this:

function tractags_rewrite($hook, $entity_type, $returnvalue, $params)
{
global $CONFIG;

$view = $params['view'];

if (($view) && (in_array($view, $CONFIG->tractags_views)))
{
// Search and replace ticket numbers
$returnvalue = preg_replace_callback('/(#)([0-9]+)/i',
create_function(
'$matches',
'
global $CONFIG;

return "<a href=\"{$CONFIG->trac_baseurl}ticket/{$matches[2]}\">{$matches[0]}</a>";
'
), $returnvalue);

// Search and replace changesets
$returnvalue = preg_replace_callback('/(\[)([0-9]+)(\])/i',
create_function(
'$matches',
'
global $CONFIG;

return "<a href=\"{$CONFIG->trac_baseurl}changeset/{$matches[2]}\">{$matches[0]}</a>";
'
), $returnvalue);

return $returnvalue;
}
}

I’m sure you will be able to come up with some much more interesting uses!

Image from the Trac project.

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:

<?php
/**
* Simple gravatar integration for Elgg.
* Scratching an itch! (+ a good example of icon overloading)
*
* TODO:
* 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/
*/

/**
* Init.
*
*/
function gravatar_init()
{
// 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)
{
global $CONFIG;

// 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;
if (isset($size_lookup[$params['size']]))
$size = $size_lookup[$params['size']];

return "http://www.gravatar.com/avatar/".md5($params['entity']->email) . ".jpg?s=$size";
}
}

// Initialise plugin
register_elgg_event_handler('init','system','gravatar_init');
?>

Pretty simple.