Custom RSS in Textpattern
Part One: Basics
I’ve been tinkering with RSS in Textpattern for some time now, trying to figure out how to syndicate what Ben calls a hybrid tumblog.
I won’t be covering any new ground with this post—others have written tutorials on this (See this article for an example aimed at would-be podcasters)—but I want to provide my own basic tutorial to build on in the coming weeks and months.
I use one plugin in this tutorial: mg_setheader, which instructs Textpattern to change the HTTP headers to properly identify our feed as XML instead of HTML.
To start with, here’s my standard, Textpattern-generated feed, as of right now:
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel><generator>http://textpattern.com/?v=4.0.5</generator>
<title>Daydream Lab</title>
<link>http://www.daydreamlab.com/</link>
<description>Thirty Years of Experience in Daydream R&D</description>
<pubDate>Fri, 18 Jan 2008 20:34:25 GMT</pubDate>
<item><title>St. Augustine</title>
<content:encoded>
<![CDATA[<p>“On earth we are wayfarers, always on the go. This means that we have to keep on moving forward. Therefore be always unhappy about what you are if you want to reach what you are not.”</p>]]>
</content:encoded>
<link>http://www.daydreamlab.com/thoughts/st-augustine</link>
<pubDate>Fri, 18 Jan 2008 20:02:10 GMT</pubDate>
<dc:creator>Adam Bradley</dc:creator>
<guid isPermaLink="false">tag:www.daydreamlab.com,2008-01-18:3ffa0ca248affff16e3d997bfa61988e/e76721d1433883b260acbbad6b229f3a</guid>
</item></channel>
</rss>
I’m going to reconstruct this, from scratch, in a Textpattern Page and two Forms. Here’s the page:
<txp:mg_setheader /><?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel><generator>http://textpattern.com/?v=4.0.5</generator>
<title><txp:site_name /></title>
<link><txp:site_url /></link>
<description><txp:site_slogan /></description>
<pubDate><txp:article_custom allowoverride="0" listform="rss_pubdate" limit="1" /></pubDate>
<txp:article_custom limit="10" allowoverride="0" form="rss_basic_article" />
</channel>
</rss>
Which is very simple to create. Copy and paste your feed to the page edit window, then:
- Add a
<txp:mg_setheader />tag to the top, to make Textpattern serve this page as XML. - Replace the name of your site, homepage URL, and site slogan with the appropriate Textpattern tags.
- Replace the
pubDatewith the tag<txp:article_custom allowoverride="0" listform="rss_pubdate" limit="1" />. We’ll create therss_pubdateform next. - Delete everything between the first
<item>tag and the last</item>tag—that is, all of your articles—and replace all of that with an<txp:article_custom />call which will display however-many articles you want in your feed (the call above shows ten) using the form we’re about to create.
Here’s the rss_pubdate form. It simply shows the post date in the appropriate format according to the specification.
<txp:posted format="%a, %d %b %Y %H:%M:%S %z" />
Finally, here’s the article form. The page above assumes it will be called rss_basic_article, but you can simply change that in the article_custom tag.
<item>
<title><txp:title /></title>
<description>
<![CDATA[<txp:excerpt />]]>
</description>
<content:encoded>
<![CDATA[<txp:body />]]>
</content:encoded>
<link><txp:permlink /></link>
<pubDate><txp:posted format="%a, %d %b %Y %H:%M:%S %z" /></pubDate>
<guid isPermaLink="true"><txp:permlink /></guid>
</item>
The Textpattern tags should be mostly self-explanatory—it’s probably a lot simpler than the article forms used on your web pages. Note that pubDate uses exactly the same code we used above for the feed publication date. Also:
- The form above has both
<description>and<content:encoded>sections. Textpattern only puts one of these, depending on whether you choose to syndicate full posts or just excerpts. It’s ok to put both—some feed readers, like Bloglines, will let user choose which to see. It is recommended, according to Feed Validator, that you putcontent:encoded, with your complete body, afterdescription. Of course, if you don’t want to syndicate entire posts, you can just leave thecontent:encodedsection out. guidis “a string that uniquely identifies the item.” I simply use the article permlink. Textpattern doesn’t use the permlink, but I’m not sure what it does use.
At this point, you should run your feed through a feed validator, to ensure that it works. One problem you may have is the error “XML parsing error: not well-formed (invalid token),” caused by a forbidden character, such as an ampersand, somewhere in your feed (probably your site name or slogan or an article title). These should be escaped (e.g. & instead of &), though you can also wrap the offending outputs in <![CDATA[...]]> tags, so that
<txp:site_slogan />
becomes
<![CDATA[<txp:site_slogan />]]>
I explain in part 2 how to automatically escape these characters.
That’s it for now. I expect to write part two by the end of January; if you’d like to be alerted, you can subscribe to my main RSS feed, or this Textpattern-only feed.
Field Reports:
Commenting is closed for this article.

Subscribe to the Daydream Lab [RSS]