01.25.05

Proper Feed Formatting with Textile

Tagged as

I use WordPress as my weblog engine these days. I also use the Textile humane web text generator for formatting. WordPress comes with a handy Textile 2 plugin, which works beautifully. Unfortunately, it completely ignores RSS and Atom feeds.

In other words (as some of you may have noticed), reading my blog in an aggregator until recently yielded you the raw, unformatted Textile markup, not the beautiful, humane, formatted text. Links didn’t work, nothing was bold or italicized, and things just generally looked awful.

Turns out there’s a very simple fix—all you need to do is add two filters so that outgoing feeds are parsed by the Textile plugin. But if you change the original plugin file (textile2.php, located in your plugins folder), your changes will be overwritten if the textile plugin is updated. And you don’t want that.

Instead, you want to use a my-hacks.php file. The details are on the wiki and in this support thread, but here it is in a nutshell: make sure the “Use legacy my-hacks.php support” option (under Options -> Miscellaneous) is checked. Then create a file in your root folder called my-hacks.php (with permissions ensuring the server can read it) containing the following code—and only this code. Extra lines before and after the PHP code block can and will foul things up, so make sure there are no blank lines or other garbage in there. Also, please note that I’ve only tested this on WordPress 1.2, because that’s the only version I’ve ever used as of this writing.

<?php
    add_filter('the_content_rss', 'textile', 6);
    add_filter('the_excerpt_rss', 'textile', 6);
?>

Unfortunately, as mentioned in the support thread linked above, this will create another problem: Atom feeds won’t validate because they have a mode="escaped" attribute on their <content> and <excerpt> elements, and that mode conflicts with the XHTML entities that now appear in our Atom feed. The fix for this is a little bit messier: open up wp-atom.php and delete the two occurrences of the string mode="escaped". You’ll note that we’re not using my_hacks.php here, so the next time you update WordPress, this change will be lost and you’ll probably need to do it again (unless, of course, the next version of WordPress fixes the problem).

The result should be nicely formatted RSS feeds for your loyal readers. Also, note that although the name of the filter only mentions RSS, this hack does work on Atom feeds, too. Enjoy!

Leave a Comment