grav-plugin-feed icon indicating copy to clipboard operation
grav-plugin-feed copied to clipboard

<image> data

Open parrisgjerde opened this issue 4 years ago • 5 comments

Is there a way to modify feed.rss.twig to include a feature image for the RSS feed?

parrisgjerde avatar Feb 03 '21 20:02 parrisgjerde

You can certainly override the twig template in your theme to add any functionality you want. However please note you should test it to make sure it's still a valid feed or your users might not be able to consume it.

rhukster avatar Feb 03 '21 20:02 rhukster

Thanks

parrisgjerde avatar Feb 03 '21 21:02 parrisgjerde

Ciao, did you manage to include images in RSS? a colleague created a plugin for WP to just add the image, https://github.com/lesion/add-img-to-feed - still looking for a viable solution for grav. We manage to display images into grav feeds using PHP8 + TwigFeed beta https://github.com/OleVik/grav-plugin-twigfeeds/releases/tag/v5.0.0-beta.2 but the necessary twig is far from good - using item.content instead of item.medias - any idea will be appreciated, tnx.

karless1 avatar Aug 30 '22 16:08 karless1

If you are asking about an img that is highlighted for each post, that's already being done via the feed.rss.twig file:

this gets the first image from the page the entry is based on: https://github.com/getgrav/grav-plugin-feed/blob/develop/templates/feed.rss.twig#L20

and this displays it if it's available: https://github.com/getgrav/grav-plugin-feed/blob/develop/templates/feed.rss.twig#L28-L30

If you wanted to provide another image, or provide a URL you could simply add some custom header to the page. Depending on if you want to specify an image in the page folder, or an image in the site 'somewhere' or even an external image, you could override the feed.rss.twig file in your theme and replace the {%set banner... variable bit with something like:

Assuming you specify an external in your page header like this:

feed:
  image: https://source.unsplash.com/gySMaocSdqs/1200x800

or a local image via stream like this:

feed:
  image: user://images/foo.jpg

NOTE: This resolves to user/images/foo.jpg in your filesystem

and then copy and modify the feed.rss.twig file in your theme like this:

{% set banner_file = item.header.feed.image %}
{% banner_file %}
  {% if banner_file starts with 'http' %}
    {# assume external file and just pull the img in as-is #}
    {% set banner = '<img src="' ~ url(banner_file) ~ '" alt="featured img" />' %}
  {% else %}
    {# assume a local file via a stream and we'll use media to load image to allow resizing #}
    {% set banner = media[banner_file].cropZoom(1200,800).html|absolute_url %}
  {% endif %}
{% else %}
  {# fallback is to grab first page image and resize it #}
  {% set banner = cropZoom(1200,800).html|absolute_url %}
{% endif %}

Then we just need to update the bit where it outputs a little as we now resize the image in the variable definition above:

<![CDATA[
{% if banner %}
 {{ banner|raw }}
{% endif %}

Something like that. Not tested, but you get the idea.

rhukster avatar Aug 30 '22 16:08 rhukster

ciao, many thanks for your contribution! great! I was tinkering with, but you make it far more easier. I'm testing what you wrote.

Still,

the grav rss template is good for us, but, for our purposes it lacks an enclosure for the image or even better for a media:image tag because right now, AFAIK, it includes the image into the content and it's not trivial to pick it up - somethin like a safe_truncate_html from content as in https://github.com/getgrav/grav-plugin-feed/blob/13606c7c2ee470a543746e991fc39b530f9e6751/templates/feed.rss.twig#L31

this was the main reason leading us to re-code another WP plugin, add just a media:image https://github.com/lesion/add-img-to-feed/blob/nomaster/add-img-to-feed.php#L47

karless1 avatar Sep 01 '22 11:09 karless1