Creating an RSS Feed
Although not as important as it used to be in the past, an RSS feed is still a standard part of most blogs. Kirby doesn’t come with RSS capabilities by default, but it can easily be added. This also allows us to explore “plugins” in Kirby.
Inside of the “site” directory, you might have already spotted that empty “plugin” subfolder. Well, its empty days are over! Before we start hacking our own RSS feed plugin, however, let’s see if someone else hasn’t already done this for us: welcome to the Kirby plugin directory.
Installing the RSS Feed Plugin
And it seems we’re indeed lucky: the “RSS Feed Plugin” is exactly what we need.
Installation is as simple as throwing those “feed.php” and “template.php” files into a new “feed” subfolder in “plugins”.
The “template.php” file determines how our RSS feed will be rendered. In most projects, there will be no need to change anything. However, if you wanted to, making changes is quite straightforward: this template works much like the ones we worked with earlier.
To make an example, if your posts had an additional “Subtitle” data field, you could simply modify the <title> node as follows:
<?php $titlePlusSubtitle = $item->title() . '-' . $item->subtitle() ?> <title><?php echo xml($titlePlusSubtitle) ?></title>
In case you want to get a feel for the inner workings of the plugin, you can also have a look at “feed.php”. However, understanding and creating Kirby plugins (although fairly simple) is beyond the scope of this course.
A Page for Our RSS Feed
At the moment, the RSS feed plugin isn’t used anywhere in our project, yet. We need to create both a page item and a template to change this.
Creating the page is very easy: in the “content” directory, we create a “feed” folder with an empty “feed.txt” file.
This will make our feed available via http://localhost:8888/feed
But not just yet…
A Template for Our RSS Feed
Currently, Kirby can’t find a template for a “feed.txt” page. Therefore, it uses the “default.php” template - which is clearly not what we want.
Let’s create a custom “feed.php” file in site/templates/. The plugin’s GitHub page already proposes a very sensible default template:
<?php echo page('blog')->children()->visible()->flip()->limit(10)->feed(array( 'title' => 'Latest articles', 'description' => 'Read the latest news about our company', 'link' => 'blog' ));
The configuration is rather simple:
- The plugin starts with the “blog” page (which we’ll have to change to “posts” in a minute)…
- and collects all visible (i.e. numbered) child pages herein…
- changes their sorting order (so that newest posts are first)…
- and supplies just the latest 10 items to the “feed” method (which was added by our plugin).
Let’s quickly replace “blog” with “posts” so that the page function has the correct folder to look for our post items. You can delete the ‘link’ parameter since we want our main feed to point at our homepage (which is the plugin’s default).
Opening the feed page in our browser (http://localhost:8888/feed) should now show our wonderful feed: