All in the <head>

– Ponderings & code by Drew McLellan –

– Live from The Internets since 2003 –

About

Textpattern Plugins

6 April 2004

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:

<txp:dru_random_image category="donkies" />

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:

<txp:dru_recent_referers limit="10" break="li" wraptag="ul" />

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.

<txp:dru_chatometer limit="10" wraptag="ul" break="li" />

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.

<txp:dru_random_text source="file" path="/path/to/text/file" delimiter="|" />

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

<txp:dru_random_text source="database" table="textpattern" column="Title" />

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.

- Drew McLellan

Tags

Comments

  1. § Mike Jones: Oh you are a lovely fella writing all these plug-ins for the community… Makes me contemplate moving to TXP to escape the comment spam in MovableType… or should I just wait for 3.0 … or should I just write my own… joke!
  2. § Michael Wexler: Don’t forget about the setting in admin_config.php. Beyond “enabling” plugins in the web gui, for the first one, I had to change the “enable plugins” setting from 0 to 1 by editing admin_config.php. Then everything started working…
  3. § Drew: That’s right, Michael. Thanks for the reminder :)
  4. § Matt Kelley: Very nice plug-ins. Thanks.
  5. § Hans: You typed this:
    ...but the default is one item per line (omit the delimited attribute for one-per-line).

    It’s delimeter. Sorry, I just can’t stand typos when I see ‘em.

    Oh, and great plugins, too. Especially the random image one… I might use that for my header. maybe
  6. § Nathan Pitman: How about a plug-in to display related articles, while allowing the user to specify a ‘section’ as with your dru_recent_articles plug-in…?

    That would be groovy!

    :)
  7. § Nathan Pitman: I came across some more Textpattern plug-ins:

    http://www.nathanpitman.com/bio/index.php?id=39
  8. § Jean François Porchez: I use Recent Referers there and its great. thanks for it.
  9. § Max Ziebell: The referer script needs to be fixed see:
    http://weblogs.asp.net/jezell/archive/2004/05/26/142045.aspx
  10. § Drew McLellan: I guess that’s right. This line:

    $out[] = tag($domain,'a',' href="http://'.$refer.'" title="Recorded: '.$ref_time.'"');

    needs to be replaced with:

    $out[] = tag($domain,'a',' href="http://'.htmlspecialchars($refer).'" title="Recorded: '.htmlspecialchars($ref_time).'"');

    before enabling the plugin.

    I’ll update the download today at some point.
  11. § Drew McLellan: Download updated.
  12. § Joni: I just wanted to say that I’m using your Random Image plugin—the thumbnail only—in my sidebar to showcase a random web design of mine. I love this plugin. Took me a couple of minutes of head scratching and two cups of coffee to figure out how to get it to do the thumbnail only and not the image, but that’s because I’m new to the textile language.

    Great work!
  13. § David: Great plugins, especially like dru_recent_article, saved me a lot of work ! THX :-)
  14. § Drew McLellan: With regard to Recent Referers, Matt Lyon writes:

    I recently installed your recent referrers plugin for textpattern… however it for some reason was displaying empty refer listings, when your code seems to request it doesn’t.

    So, I changed the $rs = getRows command to not use isnull, but instead (refer like '%.%'), with the idea that any valid referrer will have a dot in it, surrounded by text on two sides.
  15. § Matt Lyon: Hi Drew, me again. :)

    Another problem cropped up where a TxP would record some referrers with an extra // in front of them. So I added the following line:

    $refer = str_replace(’//’,’’,$refer);

    in the foreach loop after “extract($a)”

    I’m running TxP rev 57, I have no idea if this is some temporary bug of Dean’s, but there you have it. I am a total PHP newbie, so please forgive me if using a variable to compute itself is bad form.
  16. § Alan: Hi Drew. I use several of these plugins – thanks!

    Just noticed though that in the upgrade from RC1 to the RC3 that chatometer now seems to be using “messy” URLs instead of the “clean” ones it used to before (and that I use everywhere else on my site). Not a massive issue, but over time might cause some schizophrenia in search engine results…
  17. § Gramofony: Great,thanks.
  18. § Vexeffexx: Great plugins, thanks!
  19. § Konstantin:

    I have used your plugin “RandomText” on my site, but it could show russian (cyrillic) symbols I see. Is it bug?
    I recieve something like this: ���������� ������ 2, instead Интересная цитата 2

  20. § Bruno P.:

    I wanted to make your random quote work for a big 7000 records table I had… and I maybe found a small bug in the plugin script :

    On line 12 (or 13… ha ha), you wrote :

    if($rs) return $rs[O];

    I changed this to :

    if($rs) return $rs[$column];

    Well it’s not a bug, but when you have a table
    with more than 1 column, it works fine like
    this (got PHP5).

    I’ll modify the plugin to have the author (another column) printed also. Can send you the final release if you’re intersted.

    Bruno.

  21. § mong:

    Thank for great Chatometer plugin!

Photographs

Work With Me

edgeofmyseat.com logo

At edgeofmyseat.com we build custom content management systems, ecommerce solutions and develop web apps.

Follow me

Affiliation

  • Web Standards Project
  • Britpack
  • 24 ways

I made

Perch - a really little cms

About Drew McLellan

Photo of Drew McLellan

Drew McLellan (@drewm) has been hacking on the web since around 1996 following an unfortunate incident with a margarine tub. Since then he’s spread himself between both front- and back-end development projects, and now is Director and Senior Web Developer at edgeofmyseat.com in Maidenhead, UK (GEO: 51.5217, -0.7177). Prior to this, Drew was a Web Developer for Yahoo!, and before that primarily worked as a technical lead within design and branding agencies for clients such as Nissan, Goodyear Dunlop, Siemens/Bosch, Cadburys, ICI Dulux and Virgin.net. Somewhere along the way, Drew managed to get himself embroiled with Dreamweaver and was made an early Macromedia Evangelist for that product. This lead to book deals, public appearances, fame, glory, and his eventual downfall.

Picking himself up again, Drew is now a strong advocate for best practises, and stood as Group Lead for The Web Standards Project 2006-08. He has had articles published by A List Apart, Adobe, and O’Reilly Media’s XML.com, mostly due to mistaken identity. Drew is a proponent of the lower-case semantic web, and is currently expending energies in the direction of the microformats movement, with particular interests in making parsers an off-the-shelf commodity and developing simple UI conventions. He writes here at all in the head and, with a little help from his friends, at 24 ways.