All in the <head> – Ponderings and code by Drew McLellan –

Textpattern Plugins

One of the most powerful features of established blogging/small CMS tools like Movable Type is the wealth of plugins that are available to add additional functionality to the software. Textpattern, still young, has only just gained the ability to install plugins into its architecture, so I’ve taken the opportunity to write some of those easy and obvious plugins that every CMS is duty bound to have.

Textpattern plugins are very simple to implement. A plugin consists of a single function, the name of which is used as a template tag. Any attributes to that tag are passed to the function as parameters. Stored in the database, the plugin functions are retrieved at run-time and are executed within the scope of the primary functions – you’re directly in the engine room. This makes plugins very quick to develop, as all the standard functions that TXP uses are also available to plugins.

Here’s a few I’ve created so far. Typically they’ve taken about an hour each, but this includes my learning time and the time it takes to establish a workflow in something new. I will get quicker.

Random Image by Category picks an image at random from those the user has uploaded through TXP. By specifying a category, the user can limit the selection to any single image category as defined within their setup. The template tag looks like this:

Recent Referers works much in the same way as TXP’s own recent articles and recent comments tags. It digs through the user’s referer logs and brings back a list of the most recent. As is standard in TXP, the resulting XHTML can be controlled through a basic set of attributes:

Chatometer is again very similar. Its purpose is to list articles by the number of comments they receive. It gives some idea of which articles are the busiest in terms of dicussion, which is often a good pointer to the most interesting articles. Forgive the name.

Random Text grabs a random text string from either a database table or a text file. This could be useful for all sort of things from a quote-of-the-day to a random site strap line, to anything really.

To randomize from file, you simply upload a text file to your site containing the items to be picked from. You can specify your own delimiter, but the default is one item per line (omit the delimiter attribute for one-per-line). Set the source to “file” and the path to the full path to your file.

To randomize from a database table, the plugin picks a named column from a random row in a named table. The table needs to be in the same database that TXP is using. You specify table and column attributes, and set the source attribute to “database”.

A new plugin is disabled by default after it is installed. The user must then switch plugin on before it is ever run on their live site. The wise will remember to check the contents of a plugin before enabling it, as the consequences of a malicious plugin could be dire. I believe Dean has plans for an approved plugins scheme, which would help tremendously in this respect.

So there we have it. Plugins in Textpattern.