Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSS with featured images #17

Open
KMcLeodDC opened this issue Dec 15, 2023 · 1 comment
Open

RSS with featured images #17

KMcLeodDC opened this issue Dec 15, 2023 · 1 comment

Comments

@KMcLeodDC
Copy link

KMcLeodDC commented Dec 15, 2023

Been trying to implement this without success. Not certain if the theme lacks support for it or I'm overlooking a solution.
If the theme doesn't currently support for RSS with images, can it be added?
If it does, can your documentation include a description of how to implement it? |
Preferably without having to resort to plugins, but support for plugins would be an improvement.

@GwynethLlewelyn
Copy link

I would think that this is something not directly related/supported by themes themselves, but rather by additional code and/or plugins.

I wonder — did you try to implement any of the suggestions proposed on [https://smartwp.com/featured-image-wordpress-rss-feed/](this article)? Because URLs tend to disappear, let me point out two of the solutions they suggest:

1. Manually add a filter (usually to functions.php)

Credits to @someguy9 who publicly shared this gist:

<?php
// This will prepend your WordPress RSS feed content with the featured image
add_filter('the_content', 'my_featured_image_in_rss_feed');
function my_featured_image_in_rss_feed( $content ) {
  global $post;
  if( is_feed() ) {
    if ( has_post_thumbnail( $post->ID ) ){
      $prepend = '<div>' . get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'margin-bottom: 10px;' ) ) . '</div>';
      $content = $prepend . $content;
    }
  }
  return $content;
}

This should go straight into your functions.php file (in a child theme, to make sure that any upgrades don't break your changes!). Alternatively, you can use WPCode or Code Snippets or any such plugin to do essentially the same without changing your functions.php file (and risk it being overwritten).

Quoting from WPCode's plugin page:

Most WordPress tutorial websites ask you to add code snippets to your theme’s functions.php file. This makes managing code snippets messy, and it also prevents you from updating your theme.

If you ever update your theme or switch to another theme, then you will lose all custom code functions that you added in your functions.php file.

Note that if you change the functions.php on a child theme, you'll be 'safe' from any upgrades to the 'main' theme. But if you switch themes, well, then you need to remember to copy that code over to the new theme. Having a code snippets insertion plugin makes the code independent from whatever theme you're using.

2. Create a "must-use" plugin

I'm pretty sure that this is not the best way for doing this, but I just love must-use plugins (also known as mu-plugins), because they're so simple to write!

All you need to do is to create a new file under .../wp-content/mu-plugins — use whatever name you wish, so long as it has a PHP extension, of course — and prepend the code above with the following comment (it goes right on the first line after the opening <?php tag):

/*
Plugin Name: RSS with featured images
Description: While this 'must-use' plugin is active, all your RSS feeds will include the featured image.
Author: Insert Your Name Yere
Version: 1.0
Author URI: https://insert-your-website-url.here
*/
// now copy the filter code here...

It's that simple! There, you have a plugin that nobody (not even the administrator) can remove, change, or otherwise tamper with; it will show up as a plugin (thus the importance of actually writing a description — so that you remember what it's for) and will work exactly like a full-blown plugin — minus all the bells and whistles (and the thousands of lines of code usually found even on the simplest plugins these days).

As said, this works, but I'm pretty sure that 'must-use' plugins were never meant to be used for these kind of things... and that this suggestion will be frowned upon by 'serious' WordPress developers. But as a quick & dirty, persistent solution, it's perfect! And it's totally under your control, since it's a plugin written by you and specifically installed by you — you know what it does, how it does it, and what to do in case something goes wrong (e.g., if you get a White Page of Death or some kind of error, just remove the mu-plugin and analyse it thoroughly for errors/bad copy-pasting/wrong permissions on disk, etc.)

3. Use a 'serious' plugin...

There are many of those around. I have personally never used any of them, but Featured Images in RSS for Mailchimp & More, recommended on the article before, looks good to me. Unlike the other "quick & dirty" methods, this plugin has its own configuration panel and so forth, so you have much more control over how the image will be shown (and where), without requiring any coding — just pressing buttons, clicking on checkboxes, filling in a few form fields. Since it's a plugin, you won't need to worry when you upgrade themes or even switch them — whatever code is injected by this plugin to make the magic happen will continue to work. On top of that, you'll probably get some form of support, just in case something goes wrong, via either the WP forums, or — possibly only with a paid upgrade — from the developers themselves.

There are other plugins out there, just have fun looking through the choices in the WP Plugin Library, and pick the one you like best!

Good luck :)

Caveats

Although you can place images on a RSS feed using the code above (i.e., using a <div>), this doesn't mean that such images will be displayed by all RSS readers. In fact, most will probably completely ignore the image. You might have better luck when displaying the RSS feed from within a Chromium-based viewer, but that's also not guaranteed — it's a non-standard way of showing images if the RSS reader is able to display the content of the item as valid HTML. This is assumed to be the case (due to a very early implementation of a popular RSS reader)

The proper way to do it is by using the RSS-specific <enclosure> tag. This tag allows any multimedia content to be "attached", so to speak, with each RSS item. This tag has been made famous by Apple, when it decided to use RSS feeds for Podcasts, using <enclosure> to point to the actual media file, and the rest of the markup for the metadata. But it can me applied — in a standardised way! — to any kind of multimedia content, including video and images, and RSS-compliant readers will know what the <enclosure> stands for (as opposed to a <div> tag, which only makes sense inside the context of an HTML page).

<enclosure
 url="http://my.web.site/thumbnail001.jpg" 
 length="56509"
 type="image/jpeg
/>

I'm not quite sure if you can take the code above and simply replace it with enclosure tag above... I haven't tried it out myself, but it's worth giving it a try, and see if it works for you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants