How to Add an RSS Feed to Your Kirby Blog
Here's a quick and simple way of adding an RSS feed to your Kirby based blog without using plugins.
I've been using Kirby for a little over a year now, and I'm still really enjoying it. However, along the way there are lots of things I've learned that I haven't really documented very well. So I thought I'd start writing them up as posts, mainly so that I can refer to them, but also (hopefully) to help some of you.
In this first post, I'm going to explain how you can add an RSS feed to your Kirby blog without the need for installing plugins.
Step 1 - create the RSS snippet
The first thing you need to do is create a new file called rss.php
in your snippets folder. By default, that location should be:
[site root]/site/snippets/
You then need to paste the contents of this file into the rss.php
file you just created.
Once you've pasted it in, you need to make sure that the date()
field on lines 9 and 22 matches the name of the date field in your blog post blueprint.
For example, if you've used published
as the name for your date field, line 9 would look like this:
<lastBuildDate><?= $posts->first()->published()->toDate('r') ?></lastBuildDate>
What this snippet does, is loop through your posts and pull the title, published date and all the content (that's very important).
Now Kirby has the data it needs for the RSS feed, we need to tell it how to use it.
Step 2 - create a route
The next step is to create a route in your config file so that Kirby is expecting to see the RSS feed and knows what to do with it. To do this, open up your config.php
file and add the contents of this file to the routes
section.
Once you've done that, you need to edit a few lines within the route:
- Line 9 is where you set the URL of your feed. This will default to
/feed
, but if you want to change the URL, change the/feed
part of this line - On lines 12 & 13 edit the details so that it has the name of your blog and a short summary
- Line 14 configures how many items will be in your RSS feed. This defaults to 20, but if you want to have more or less, change the
limit(20)
value
That's literally it. By following these few simple steps you should have a valid RSS feed that's easy for you to configure. For example if, like me, you want to add a little message to the end of your RSS feed items, you can add something right before the closing </description>
tag.
So this:
<![CDATA[<?= $item->text()->kt() ?>]]>
</description>
Becomes this:
<![CDATA[<?= $item->text()->kt() ?>]]>
<![CDATA[
<hr>
<p>Thanks for reading this post via RSS. RSS is great, and you're great for using it. ❤️</p>
]]>
</description>
Final thoughts
There are a number of plugins available to add RSS support to Kirby, but I tended to have problems with the way these plugins created the feed, and its validity. With it being a plugin, troubleshooting was difficult, so I decided to build my own.
This solution is simple so you shouldn't introduce any site breaking code, and it gives you the flexibility to customise the feed as needed.
I've create a GitHub repository to supplement these posts, so I will add to that as I continue to document this stuff.
In the meantime, if you have a Kirby blog and are having problems, I'm happy to help if I can, just drop me an email using the button below
Want more?
So you've read this post and you're still not satisfied? Ok then, here's some other stuff for you to do: