Textpattern tips, tutorials and code snippets

Create an RSS Feed for last comments

Credits

This tip has been first posted in the forum, using a completely other logic. This was immediately followed by a big hint from MattD, showing the right direction TXP Tip on creating an author feed

Pre-requisites

Two plugins have to be installed and activated :

Step-by-step

  • go to Admin Extensions tab > External output > Create a new output
  • name it whatever you want (for example here: commrss)
  • give text/xml as Content-type
  • copy-paste the following code:
<?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/" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
 <generator>http://textpattern.com/</generator>
 <title><txp:site_name /></title>
 <description><txp:site_slogan /> - comments</description>
 <link><txp:site_url /></link>
 <atom:link href="<txp:site_url />?rah_external_output=commrss" rel="self" type="application/rss+xml" />
 <pubDate><txp:php>echo safe_strftime('rfc822');</txp:php></pubDate>

 <txp:recent_comments break="">

   <txp:variable name="item_content" value='<![CDATA[<txp:comment_message />]]>' />
   <txp:variable name="item_author" value='<txp:etz_striptags><txp:comment_name /></txp:etz_striptags>' />
   <txp:variable name="link_guid" value='<txp:php>global $thiscomment; echo permlinkurl($thisarticle)."#c".$thiscomment["discussid"];</txp:php>' />

   <item>
     <title>Comment on <txp:title no_widow="0" /> by <txp:variable name="item_author" /></title>
     <link><txp:variable name="link_guid" /></link>
     <guid><txp:variable name="link_guid" /></guid>
     <pubDate><txp:comment_time format="rfc822" /></pubDate>
     <description><txp:variable name="item_content" /></description>
     <content:encoded><txp:variable name="item_content" /></content:encoded>
     <dc:creator><txp:variable name="item_author" /></dc:creator>
   </item>

 </txp:recent_comments>

</channel>
</rss>

Recent comment are retrieved with <txp:recent_comments>, data are stored once and for all using <txp:variable>, then rendered between the tags <item> ... </item>

To retrieve the feed :

  • in a textpattern form or page: <txp:site_url />?rah_external_output=commrss
  • “hard coded” (where www.example.com is your domain name): http://www.example.com/?rah_external_output=commrss

4 Comments Comment feed

That feed’s URL could be made prettier by inserting to the site’s .htaccess file something like this:

RewriteRule ^comments.xml$ ./index.php?rah_external_output=commrss

The comments.xml can be changed to whatever is needed. It can be a folder:

RewriteRule ^comments-rss/$ ./index.php?rah_external_output=commrss

A better syntax for the author's comment name :

Instead of <txp:variable name="item_author"/> (no longer needed)
use <txp:comment_name link="0"/>

Thanks Claire - just to confirm - you'd like this line changed from:

<txp:variable name="item_author" value="&lt;txp:etz_striptags&gt;&lt;txp:comment_name /&gt;&lt;/txp:etz_striptags&gt;"/>

to:

<txp:variable name="item_author" value="&lt;txp:etz_striptags&gt;&lt;txp:comment_name link="0"&gt;&lt;/txp:comment_name&gt;&lt;/txp:etz_striptags&gt;"/></txp:variable>

Mmm. Annoying that Disqus changes < or > into an HTML entity even within a code block!

Add a comment

Use Textile help to style your comments