Recently, I wanted to add content to each item in the RSS feed on a WordPress site. This could be something like:
Hire Sal
If you enjoyed this article, have you considered hiring Sal to help with your project?
To add this, we can leverage the WordPress filter the_content_feed
. The code would look something like
add_filter( 'the_content_feed', 'fe_the_content_feed_add_hire_sal_blurb' );
function fe_the_content_feed_add_hire_sal_blurb( $content ) {
$hire_sal_blurb = "\n<h3>Hire Sal</h3>\n"
. '<p>If you enjoyed this article, have you considered '
. '<a href="https://ironcodestudio.com/contact-us/">hiring Sal</a> '
. "to help with your project?</p>\n";
return $content . $hire_sal_blurb;
}
Testing
There are a couple of challenges with testing RSS feeds.
Caching
Web browsers cache RSS feeds aggressively, so if you’re looking at the feed in the browser (e.g. https://salferrarello.com/feed/) your updates won’t show up in a timely manner.
If you’re familiar with Chrome developer tools, you can use it to Disable cache (while DevTools is open) to insure you are loading the most recent version. Alternatively, you can use the command line tool curl
to view the feed without caching.
curl https://salferrarello.com/feed/
Local Development
For developing locally and previewing the rendered RSS feed, I find Vienna RSS Reader to be a good tool. Generally I read RSS feeds with an online RSS Reader (Feedbin) but an online RSS Reader doesn’t work for a local feed.
Note: When using Vienna, you will have to delete the feed and re-add it to clear the cache for a feed.
Sal nice classed tonite at Lehigh Valley WordPress Meetup
Thanks Bryan. I had a lot of fun at the Lehigh Valley WordPress Meetup.
For anyone else interested, here is the link for my slides on WordPress filters.