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

Textpattern and the Technorati Link Count Widget

Late last week, Tantek Çelik announced the new Technorati Link Count Widget over on their corporate weblog. This is a little chunck of JavaScript that queries the Technorati servers to find the number of blog posts index that link to any given URL, and then shows that count on the page itself. The idea is you can place this alongside a post or article and readers will see a link to explore other content that references the content they’re currently looking at. It’s like pingback/trackback through an intermediary. As an experiment, I’ve added it to each post on this site.

What I like most about the widget is its implementation of unobtrusive DOM scripting techniques. Adding the widget to a page is a case of adding a regular HTML link element with its rel attribute set to a value of linkcount. The JavaScript will find all instances of such tags in the page and replace them out with live data, but the nice thing is that you can set the basic links to point to Technorati’s blog search for that post. If the user doesn’t have JavaScript enabled (or does bother to wait for the entire page to load and the JavaScript to kick in) they still get the same basic functionality from the link. All that is lost is the neat live count on the page.

Looking at the script itself it’s using a tidy object structure to neatly namespace its functions and variables – a practise that’s increasingly becoming essential for multiple scripts to co-exist on pages without stepping on each other’s toes. They seem to have done a good job here making sure that the widget will play nicely with whatever else is in your page.

Anyway, the purpose of this post wasn’t to prattle on about the widget itself, but to note how I implemented it in Textpattern. The widget page gives instructions for Wordpress and a few other blogging tools, but not for TXP. Essentially, all that is needed to implement the widget is the URL of the content item. Textpattern has no standard template tag for getting that – the only available tag returns a block of markup and not just the bare URL. Therefore we need to be just a little hacky. In your post template, replacing the [square braces] with normal angle brackets:

<a href=“[txp:php] global $thisarticle; echo permlinkurl($thisarticle); [/txp:php]” rel=“linkcount”>View blog reactions</a>

Update: thanks to Robert in the comments for pointing out that from Texpattern 4.0.4, you can simply do this:

<a href=“[txp:permlink /]” rel=“linkcount”>View blog reactions</a>

You’ll need to have PHP in templates enabled in order for the former to work, but we should all be running at least 4.0.4 anyway. Following that, just drop a script tag linking to the Link Count script into your page and job’s a good’en. I’ve added mine just before the closing body tag, rather than in the head. This is because the Technorati servers often seem really slow to respond, so having the script down there will minimize the negative impact on my site when that happens.

This is working for me in Textpattern 4.0.3 (not quite the newest), let me know if you spot any problems.