Generate an RSS feed for comments

How does it work?

It all begins in with the {exp:weblog:entries} tag. Whenever we use the {exp:weblog:entries} tag we can use the {entry_id} variable to get the unique ID of a entry. When we have the entry ID, we can generate a URL to our ‘rsscomments' template. In that URL we will include the entry ID (example: http://www.domain.com/index.php/site/rsscomments/4535), in this case 4535 is our entry ID. Awaiting for us in the ‘rsscomments' template is a another {exp:weblog:entries} and a {exp:comment:entries} tag that will parse the necessary information for our feed.

1. Setting up your feed template.

We begin by creating a template inside a template group of your choice. Name your template;  I named mine ‘rsscomments'.  And the template type should be ‘RSS'. When you are ready paste the following code into your template:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:admin="http://webns.net/mvcb/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:content="http://purl.org/rss/1.0/modules/content/">
<
channel>
{exp:weblog:entries entry_id="{segment_3}" disable="categories|trackbacks|pagination|memberdata"}
<title>{exp:xml_encode protect_entities="yes" }{title}{/exp:xml_encode}</title>
<
link>{title_permalink="templategroup/template"}</link>
<
description>{exp:char_limit total="500"}{exp:xml_encode protect_entities="yes" }{summary}{/exp:xml_encode}{/exp:char_limit}</description>
{/exp:weblog:entries}
{exp
:comment:entries orderby="date" entry_id="{segment_3}" sort="desc"}
<item>
<
title>{exp:xml_encode protect_entities="yes" }Comment by {name}{/exp:xml_encode}</title>
<
link>{exp:xml_encode protect_entities="yes" }{title_permalink="templategroup/template"}{/exp:xml_encode}</link>
<
guid sPermaLink="false">{exp:xml_encode protect_entities="yes" }comment-{comment_id}{/exp:xml_encode}</guid>
<
description>{exp:char_limit total="500"}{exp:xml_encode protect_entities="yes" }{comment}{/exp:xml_encode}{/exp:char_limit}</description>
<
content:encoded><![CDATA[{comment}]]></content:encoded>
<
dc:date>{gmt_comment_date format="%Y-%m-%dT%H:%i:%s%Q"}</dc:date>
</
item>
{/exp:comment:entries}
</channel>
</
rss>
So you are wondering why I am not using the {exp:rss:feed} tag? Well my method does not need it and all the variables that are available through {exp:rss:feed} are also available in {exp:weblog:entries}. And the {exp:rss:feed} tag also is limited to 1 weblog.

Lets break this down:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:admin="http://webns.net/mvcb/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
<
channel>
Here we have the usual begin of your feed and defines which RSS version and the encoding. Take note: if you have another encoding for your site please adjust the encoding="UTF-8".

{exp:weblog:entries entry_id="{segment_3}"
disable="categories|trackbacks|pagination|memberdata"}
Here we put the power of URL segments to work. In this case segment 3 is the entry ID. Also we are going to use the disable parameter to turn off unneeded stuff for performance.

<title>{exp:xml_encode protect_entities="yes" }{title}
{
/exp:xml_encode}</title>
Defining our entry title happens here. Speaks for itself I suppose. What is {exp:xml_encode}? This tag converts reserved XML characters to entities. By using protect_entities="yes" we protect existing entities from being converted. Read more about it here

<link>{title_permalink="templategroup/template"}</link>
We use this to link back to the original entry page. Normally we use the {title_permalink} tag, but I prefer using {comment_url_title_auto_path } tag since it's much shorter and more easy to use.

<description>{exp:char_limit total="500"}
{exp
:xml_encode protect_entities="yes" }{summary}
{
/exp:xml_encode}{/exp:char_limit}</description>
We must provide a description for the feed, so a easy way is using a custom field that we use for our entry. (I used for this example the ‘summary' but you can you whatever custom field you want). We also use {exp:char_limit} cause we have a limit of 500 characters in the <description> tag

{exp:comment:entries orderby="date" entry_id="{segment_3}" sort="desc"}
Here we do the same as with the {exp:weblog:entries tag}, using segment_3 of our URL to get the entry ID.

<title>{exp:xml_encode protect_entities="yes"}Comment by {name}
{
/exp:xml_encode}</title>
Here we make use of the {name} variable to display the author of the comment.

<link>{exp:xml_encode protect_entities="yes"}
{title_permalink
="templategroup/template"}{/exp:xml_encode}</link> <guid sPermaLink="false">{exp:xml_encode protect_entities="yes" }comment-{comment_id}{/exp:xml_encode}</guid>
This is an important part of your feed. Here you must decide what will happen when a reader clicks on the comment link in the feed. You have several choices:

  1. {url_title_path="templategroup/template"} - This will generate a url to the entry page.
  2. {url_or_email} - This will generate a URL (if it exists) If the URL does not exist the email will be returned.
  3. You can also generate a URL to your entry page using {comment_id} as an anchor to link directly to the comment itself. Note: this method won't work if you use pagination on your comments

In a normal feed our <guid> element would be also a permalink, but since this is a comment feed we need to choose another method to make our guid element unique, so we use the {comment_id}.

<description>{exp:char_limit total="500"}
{exp
:xml_encode protect_entities="yes" }{comment}
{
/exp:xml_encode}{/exp:char_limit}</description>
Here we must provide a description of the comment, since we don't have that we will use the comment itself as an description using the {comment} variable. We also use {exp:char_limit} cause we have a limit of 500 characters in the <description> tag.

<content:encoded><![CDATA[{comment}]]></content:encoded>
Here is we use ofcourse the {comment} variable to parse the comment itself.

<dc:date>{ gmt_comment_date format="%Y-%m-%dT%H:%i:%s%Q"}</dc:date>
And finally here we set the date and time of the comment.

 

2. Making the hyperlink to the feed.

This part is not that difficult, because it only involves making a hyperlink with a entry ID in it. Like this:

<a href="{path=templategroup/rsscomments/{entry_id}}" 
title="{title}">Comment RSS Feed</a>

What are we doing here? With the path="templategroup/rsscomments/{entry_id}" we are generating a URL to http://www.domain.com/index.php/templategroup/rsscomments/4663 in this example ‘4663' is the entry ID.
But remember that the hyperlink must be wrapped inside the {exp:weblog:entries} tag. A simple example of a template would be:
{exp:weblog:entries weblog="myweblog"}
<H1>{title}</H1>
<
p>{summary}</p>
<
p>{extended}</p>
<
a href="{path=templategroup/rsscomments/{entry_id}}" title="{title}">Comment RSS Feed</a>
{/exp:weblog:entries}

This is just a very simple example, you can customize the hyperlink the way you want (using a small rss icon as link for example).

And voila! You got yourself a dynamic feed that will display comment for each entry you want. This method can also be applied for making a RSS feed for your gallery comments. You just have to replace the weblog tag with the gallery tag and use the appropriate variables. Success!