Skip to content

Working on updating my site and converting what I had worked on for an XML RSS feed into a more current JSON feed within a Kirby CMS controller file.

License

Notifications You must be signed in to change notification settings

luxuryluke/Kirby-JSON-RSS-feed-controller

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Kirby-JSON-RSS-feed-controller

Working on updating my site and converting what I had worked on for an XML RSS feed into a more current JSON feed within a Kirby CMS controller file. https://github.com/getkirby

my redesigned site will launch soon in 2024 and will feature the JSON formatted feed. https://lukedorny.com

JSON Feed

Since you're here you should browse the available elements in the jsonfeed.org made by Manton Reese and Brent Simmons. Maybe there are a few elements you could add to this that I haven't. It looks much more flexible than the XML feed format.

Remember, we are now serving application/feed+json and the JSON feed format is 'version' => 'https://jsonfeed.org/version/1.1', also, the URL for the feed link in your header should use this PHP code:

<link rel="alternate" type="application/feed+json"
  title="<?= $site->title()->esc() ?>"
  href="<?= $site->rssfeed()->toPage()->url() ?>"  />

Of course, if you'd just like to link to your feed page directly, bypassing my silly panel page chooser for the feed, your header should use this more normal PHP code:

<link rel="alternate" type="application/feed+json"
  title="<?= $site->title()->esc() ?>"
  href="<?= page('feed')->url() ?>"  />

Setup

I created a few files to make this work:

  • /site/templates/feed.json.php
  • /site/blueprints/feed.json.yml
  • /site/controllers/feed.json.php
  • /content/feed/feed.json.txt

Edit these files

  • /site/config/config.php

Steps & Walkthrough of Code

This setup assumes several content folders of pages to round up for the Feed.

  1. In my case, I added a Pages field in the /site/blueprints/site.yml file to choose what page in my site would be the RSS feed. This might seem extra, but it works for my needs.
  2. Then in the second file above (/site/blueprints/feed.json.yml), I added a Pages field so I could select which content parents that have children I wanted to include in my feed. For example, I added /content/articles/, /content/updates/, /content/nowplaying/ so that each of their children will get into the feed.
  3. Let's sort the combined pages by date.
  4. Next I added two additional textarea fields rssblip in that blueprint for the description of the feed, as well as the text I'd like to add at the end of each Feed item, encouraging readers to get in touch or reply via email, engage on Mastodon, what have you (alternatively you could just pipe in the SEO description of your site to the top of the feed as well if you like).
  5. Then we move on to the template. The template is rather short, as you'll see. Unfortunately, creating the feed will end up being in the Controller mostly.
  6. Next the controller file. This grabs the sections we want to include, sorts them, and grabs post images that use the typical Kirby cover image method but falls back to the first image available in each post.
  7. Then the controller creates the $items array for the feed. This is an array of all of your site pages that will be included.
  8. Then it takes the cover image and wraps it in a figure, adds alt text, a figcaption that allows links using the ->kirbytext() powers, etc.
  9. In the new $content_html object it first adds in the page's text content and strips out extra new lines of code.
  10. Then it adds the cover to the beginning of that new $content_html.
  11. You can add in other field data you may have for your pages, here I have added $materials at the end, for a project I'm doing that I'll soon launch on the site.
  12. It also adds in the rssblip at the end, which has four ---- in the textarea to give it a divider from the item's content below it
  13. I added the title of each item's parent to differentiate it from other posts, too (i.e.: "Articles: ", "Updates: ", etc.)
  14. Then it assembles the $items using the various objects we've created as well as fields from each page.
  15. Returns the $items for use.
  16. You'll see all of the data come together in the template.

If you have suggestions to make this better please fork it, change it, whatever you like. Cheers!

image

— Luke

Compatibility

This was built and working within a Kirby v4.+ install.

About

Working on updating my site and converting what I had worked on for an XML RSS feed into a more current JSON feed within a Kirby CMS controller file.

Topics

Resources

License

Stars

Watchers

Forks

Languages