RSS Feed for an image collection

Oh, btw, you can get your image collection with only one line of code:

$images = page('screenshots')->children()->visible()->images();

I still get an empty <body>, with and without the enclosure and the line of code. So nothing really changed.

Pls. try to enable debugging or check where your php_error_log is located, there should be a folder called logs somewhere. What version of XAMPP is that? Are you on Windows?

Debugging is enabled, it just doesn’t seem to output anything regarding the RSS Feed. If I purposely create an error by changing $page to $pagee or something similar, it shows up in my php_error_log.txt. Visiting the Feed URL doesn’t ouput anything.

Edit: Windows, XAMPP 3.2.2 5.6.19

I’ll try to look into this later today or tomorrow.

1 Like

Sorry, there never was a “XAMPP” version like

This number may be the version of the “XAMPP Control Panel”!

Please look in line 3 of the logged text.

1 Like

I couldn’t get it to work with the plugin. I came up with an alternative using a simple template instead:

<?php header::type('text/xml') ?>
<?php
  $items = page('screenshots')->children()->images();
  $title = page('screenshots')->title();
  $description = page('screenshots')->text();
  $generator = kirby()->option('feed.generator', 'Kirby');
  $link = page('screenshots')->url();
  $url = url();
?>
<!-- generator="<?php echo $generator ?>" -->
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom">

  <channel>
    <title><?php echo xml($title) ?></title>
    <link><?php echo xml($link) ?></link>
    <generator><?php echo c::get('feed.generator', 'Kirby') ?></generator>
    <lastBuildDate><?php echo date('r', time() ) ?></lastBuildDate>
    <atom:link href="<?php echo xml($url) ?>" rel="self" type="application/rss+xml" />

    <?php if(!empty($description)): ?>
    <description><?php echo xml($description) ?></description>
    <?php endif ?>

    <?php foreach($items as $item): ?>
    <item>
      <title><?php echo xml($item->title()) ?></title>
      <link><?php echo xml($item->url()) ?></link>
      <guid><?php echo xml($item->id()) ?></guid>
      <pubDate><?php echo $item->modified('r') ?></pubDate>
      <description>
        <enclosure url="<?= $item->url(); ?>" length="<?= $item->size(); ?>" type="image/<?= $item->extension(); ?>" />
        <![CDATA[<?php echo $item->caption()->kt() ?>]]>
      </description>
    </item>
    <?php endforeach ?>
 </channel>

</rss>

Hope this helps. You probably have to adapt this as the pub date here does not work nor does $item->id() output anything.

1 Like

@Swissendo:

To get, what I think you want, I would do the following steps:

install Kirby starterkit

install the Kirby panel

install the plugin feeds from Bastian

add a new templates file

  • add a new textfile, name it /site/templates/feed.php
  • the content should look like:
<?php
echo page('projects')->children()->visible()->flip()->limit(10)->feed(array(
  'title'       => 'Latest news',
  'description' => 'Read the latest news',
  'link'        => 'projects'
));


and save it as “UTF-8 without BOM”, e.g. by using Notepad++

add the feed

  • add a new directory /content/2-projects/feed
  • add there a new textfile, name it /content/2-projects/feed/feed.txt,
    first it needs no content

add a link to the feed

for simplicity I’m doing this as follows (later you can do it where and how you want):

  • go in the panel
  • open the homepage
  • add there in the field “Text”:
## The feed
- Open (link: projects/feed text: the feed target: projects_feed)

## first test of your feed

at this step the feed don’t contains any picture, but it should work, if you have followed my steps to show the text of the three projects pages

  • open the homepage of your new website
  • click on the new link “the feed”
  • in my browser the feed opens in a new tab, but if you use a feed browser, it may opens there
  • if not, compare your and my steps to get this running

now I want to change the feed to show the pictures from the 3 projects pages

changes in the feed-template

  • open the file /site/plugins/feed/template.php in an editor like Notepad++
  • look for the line
      <description><![CDATA[<?php echo $item->{$textfield}()->kirbytext() ?>]]></description>
  • change it to
      <description><![CDATA[<?php foreach ($item->images() as $i) {
        echo '<p><img src="' . $i->url() . '" alt="' . $i->name() . '"></p>';
      }; ?>]]></description>

  • save this page in the panel

next test of your feed

  • open the homepage of your new website
  • click on the new link “the feed”

Good luck!

P.S .: now you want to change the file feed-template ( file /site/plugins/feed/template.php ) to fit your needs. Later remember to your changes in this file!

2 Likes

@anon77445132:

It is no problem to add images to the feed, but as explained above, the requirement here is a feed based on the images, not on pages.

I do have to say your answer is very well detailed and written ! Thanks for taking the time to contribute this way!

After I delete some chars, it now only feeds the images.

The rest should be to change the first code line in the file /site/templates/feed.php.
It should filter and sort the images instead of the pages to fit the needs…

[Added:]
And add the date and time of the oldes image, that shall be shown, in a new line of the the file /site/templates/feed.php to know it in the template. Add in the template an “if” condition to only show newer images.

Well, as @Thiousi said, thanks for contributing. However, we need to get the last 10 images based on their modification date. You can’t get them like that based on pages, I’m afraid.

Mhm, interesting idea. I’ve adapted your code to my needs/wishes and now the feed looks and acts like it should, which is a major breakthrough! It isn’t a “valid” feed though, according to several Feed Validators. The main problem seems to be the pubdate/lastBuildDate, which aren’t in the correct format. The <lastBuildDate> currently looks like this:

<lastBuildDate>1469470566</lastBuildDate>

Instead of being in a format that complies with RFC-822 date-time. Same problem for the <pubDate>, which currently is empty. Is there anything I can do to get a “proper” date for each item and the feed itself? I’m currently reading up on this topic and comparing my template with the Podcast Plugin, but both seem to work with pages instead of files, so I’m stuck again. I’ll also have to yet test the feed on my live site, just to be shure this solution works fine with IFTTT/Feedly. Still, this helped a lot, thank you so much for this (possible) solution!

(By the way, the guid/id itself wasn’t as big of a problem, I simply changed

<guid><?php echo xml($item->id()) ?></guid>

to

<guid isPermaLink="false"><?php echo xml($item->hash()) ?></guid>

which seems to work fine.)

Yes, you can convert is like this:

<?php echo date('r', time()) ?>

And for the images, you could use their modification date:

<?php echo date('r', $item->modified()) ?>
1 Like

Thank you for your detailed contribution! As the others have mentioned, this doesn’t really work for my case, but it’s an excellent resource for anyone who “just” wants to get all images.

1 Like

BTW.

$item->modified('r')

works as well, I just forgot to echo it, I have corrected it above.

1 Like

Awesome,

<lastBuildDate><?php echo date('r', time()) ?></lastBuildDate>

and

<pubDate><?php echo $item->modified('r') ?></pubDate>

seem to work fine. I’ll test it on my live site and report back if everything works as intended.

I saw you wanted to add a <media: content> tag but got a namespace error. If you add the namespace to the feed, it should work:

<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
1 Like

Okay, I’ve tested the feed with various validators, readers and IFTTT, everything works fine! Thank you so much for your help, @texnixe (and @Thiousi and @anon77445132)! :tada:

For anyone else interested in creating such a feed, here’s a quick guide:

  • Add a folder named feed to the directory of your choice, for example: /content/screenshots/feed
  • Create a .txt file inside this folder, name it however you want, for example: screenshotfeed.txt
  • Create a .php file inside template, name it after your .txt, for example: /site/template/screenshotfeed.php
  • Paste the following content into the .php file and edit it if necessary:

screenshotfeed.php

<?php header::type('text/xml') ?>
<?php
  $items = page('screenshots')->children()->visible()->images()->sortBy('modified', 'desc');
  $title = page('screenshots')->title();
  $description = page('screenshots')->text();
  $generator = kirby()->option('feed.generator', 'Kirby');
  $link = page('screenshots')->url();
  $url = url();
?>
<!-- generator="<?php echo $generator ?>" -->
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom">

  <channel>
	<title><?php echo xml($title) ?></title>
	<link><?php echo xml($link) ?></link>
	<generator><?php echo c::get('feed.generator', 'Kirby') ?></generator>
	<lastBuildDate><?php echo date('r', time()) ?></lastBuildDate>
	<atom:link href="<?php echo $page('screenshots')->url() ?>/feed/" rel="self" type="application/rss+xml" />

	<?php if(!empty($description)): ?>
	<description><?php echo xml($description) ?></description>
	<?php endif ?>

	<?php foreach($items as $item): ?>
	<item>
	  <title><![CDATA[
		<?php if($item->title() == ''): ?><?php echo $item->name() ?><?php else: ?><?php echo $item->title() ?><?php endif ?>
	  ]]></title>
	  <description><![CDATA[<a href='<?php echo xml($item->url()) ?>'><img src='<?php echo xml($item->url()) ?>' /></a>]]></description>
	  <link><?php echo xml($item->url()) ?></link>
	  <guid isPermaLink="false"><?php echo xml($item->hash()) ?></guid>
	  <pubDate><?php echo $item->modified('r') ?></pubDate>
	</item>
	<?php endforeach ?>
 </channel>

</rss>

It’s a little messy right now and I will continue to tweak and edit it (including adding the <media: content> tag), but it works. Again, thank you so much for your help, I would’ve never figured this out by myself.

1 Like

Glad you sorted it out !
I’ve cleaned-up the code as much as I my skills allowed it to bullet-proof it for you. Here’s my result:

<?php header::type('text/xml') ?>
<?php
  $source = page('screenshots')
  $items = $source->children()->visible()->images()->sortBy('modified', 'desc');
  $title = $source->title();
  $description = $source->text();
  $generator = kirby()->option('feed.generator', 'Kirby');
  $link = $source->url();
  $url = url();
?>
<!-- generator="<?= $generator ?>" -->
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">

  <channel>
	<title><?= xml($title) ?></title>
	<link><?= xml($link) ?></link>
	<generator><?= xml($generator) ?></generator>
	<lastBuildDate><?= xml(date('r', time())) ?></lastBuildDate>
	<atom:link href="<?= xml($source->url()) ?>/feed/" rel="self" type="application/rss+xml" />

	<?php if(!empty($description)): ?>
	<description><?= xml($description) ?></description>
	<?php endif ?>

	<?php foreach($items as $item): ?>
	<item>
	  <title><![CDATA[
		<?= xml($item->title()->or($item->name())) ?>
	  ]]></title>
	  <description><![CDATA[<a href='<?= xml($item->url()) ?>'><img src='<?= xml($item->url()) ?>' /></a><br /><br />]]></description>
	  <link><?= xml($item->url()) ?></link>
	  <guid isPermaLink="false"><?= xml($item->hash()) ?></guid>
	  <pubDate><?= xml($item->modified('r')) ?></pubDate>
	</item>
	<?php endforeach ?>
 </channel>

</rss>

Couple things of note:

  • I’ve created a variable for the source page so it’s only necessary to change it once for anyone wanting to reuse this.
  • I’ve reused code as much as possible
  • I’ve replaced <?php echo by <?= (personal preference :slight_smile: )
  • You used once $page(‘screenshots’)->url. Which I don’t think works (the $ shouldn’t be in front of page here).
  • It’s all wrapped in xml() to make sure it’s compliant.
  • Could you test this ? :smiley:

Cheers!