A simple skeleton plugin for Wordpress

Preparing for the upcoming Meandre release, as part of the larger SEASR on, I have been exploring a bit the inner machinery of WordPress to create a simple plugin for the SEASR site that could render Meandre component and flow descriptions straight out of their RDF desciptors. Actually, it turned out to be a breeze.

After being a heavy user of WordPress in several project for few years now, its extensibility keeps sparking new possibilities in my mind every time I look into any of its facets. Anyway, I am rambling. Back to the point. To hit the ground running, I started by creating a simple generic plugin that would replace a tag with parameters for something else. I looked into several freely available plugins, the one that got me going was a very simple one, the SlideShare plugin by Joost de Valk.

The plugin transforms a formated tag into code that embed a flash player for SlideShare presentation. Below, I reproduce the skeleton of a plugin that will replace the tags in your post/pages/comments for whatever you like. A tag takes the form [tag-mnemonic param1 param2] and the plugin replaces it by just a HTML list of the two parameters.

*   ###Param1###
*   ###Param2###

function sp\_plugin\_callback($match) {
    $output = SP\_TARGET;
    $output = str\_replace("###Param1###", $match\[1\], $output);
    $output = str\_replace("###Param2###", $match\[2\], $output);
    return ($output);
} 

function sp\_plugin($content) {
    return (preg\_replace\_callback(SP\_REGEXP, 'sp\_plugin\_callback', $content));
}

add\_filter('the\_content', 'sp\_plugin'); 
add\_filter('comment\_text', 'sp\_plugin'); 

That means that if you write the tag [tag-mnemonic http://foo.org http://example.org], the plugin will reformat the tag and inject the following html code

<ul>
    <li>http://foo.org</li>
    <li>http://exampl.org</li>
</ul>

where the tag originally was. And that’s it! One more thing. The only thing you may want to be aware is the filtering chain. The add_filter has a third numeric parameter. When you have multiple plugins that rely on filters to do their job, sometimes you may need to force a certain order of execution to avoid stepping on each others toes. If that is your case, you better check the add_filter documentation. It may save you some time, trust me :D