A recent project called for the ability to dynamically create RSS feeds based on user-selectable categories. ExpressionEngine lets you use the ‘category’ keyword to zero in on a particular category (or categories) when creating an RSS feed. In this project we wanted the end user to be able to select from a number of categories to create a custom RSS feed.
Note: This is a simple solution and not particularly beautiful but works well enough (except for Safari). Spaces inserted into the code block below to allow for proper display.
Demo page: http://www.smd.net.au/site/rssbuilder
There are two templates. The first calls up our categories for a particular weblog – in this case it’s our Journal section.
<h2>Journal entries:</h2>
<form action="{path="site/rssbuilder_result/"}" method="get" accept-charset="utf-8">
{exp:weblog:categories weblog="journal" style="linear" dynamic="off" category_group="2" category="not 18"}
<input type="checkbox" name="cat" value="{category_id}" />{category_name}<br/>
{/exp:weblog:categories}
<p><input type="reset" value="Reset!"><input type="submit" value="Submit"></p>
</form>
The second template is where our results get pushed. This is where the ugly stuff happens. (Explanation is below the code).
< h1 >RSS Builder Results</ h1 >
< ?php
function curPageURL() {
$pageURL = 'http';
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
? >
<p>Click below for your custom RSS feed:</p>
{exp:replace find="http://www.smd.net.au/site/rssbuilder_result/|?cat=|&cat=" replace="http://www.smd.net.au/site/journal_rss/|category/||" multiple="yes"}
<p><a href="<?php echo curPageURL();?>"><?php echo curPageURL();?></a>
{/exp:replace}
The block of PHP gets the current URL from the browser and is echoed with:
< ?php echo curPageURL();? >
This is wrapped up in the only third-party plugin you’ll need here, Lodewijk Schutte’s Find & Replace.
This takes the browser URL and replaces it with our RSS url and adds the category keyword. Anything with “?cat” and “&cat;” it replaces with the “|” piping character. Because this is the character used in the plugin we need to use the unicode version of |.
So now our link goes from looking like this:
http ://www.smd.net.au/site/rssbuilder_result/?cat=7&cat=10
to this link which can be copied into an RSS reader:
http ://www.smd.net.au/site/journal_rss/category/7|10