WordPress: how I added an RSS feed to a post, the whole story

Hdemo Magazines Team
Hdemo Magazines
Published in
5 min readNov 16, 2015

--

A couple of weeks ago I had the idea to create a post, in the Hdemo site, that showed the latest articles published on the Italiandirectory italian website.

Of course, I had no intention to update the post every week, as the content on the source site had changed. The obvious solution looked like just using the RSS feed of the source site as the content of the post. This way the automatic update to the post was guaranteed, without need for my work (I know, I’m lazy).

As is well known, among the WP default widgets, there is RSS feed which is used to publish a feed on a sidebar. Mine was not a sidebar, so this was not useful. After a bit of googling, 3 kind of solution came out:

  1. I could use the default RSS widget in a template using the template tag the_widget, as explained in the WP bible (aka the Codex): “This template tag displays an arbitrary widget outside of a sidebar. It can be used anywhere in templates.” A good solution but, as in my case I wanted to use a post, the template is not customizable. In fact, it is quite possible to customize the single post template, as the guru Justin Tadlock taught in his famous article (a little dated but still valid) “Creating single post templates in WordPress“. I actually implemented Tadlock’s workaround in another blog but, in this case, it looked like a bit too oversized. Solution rejected.
  2. I could use the standard wp function fetch_feed, which, as said in the Codex retrieves an external feed and parses it. Uses the SimplePie and FeedCache functionality for retrieval and parsing and automatic caching. Being a function, however, it can only be used in a template. Then, for the same reason above, I rejected this solution.
  3. I could use a plugin. There are several: amr shortcode any widget , WP Custom Post RSS Feed and many others. None of the ones I had seen, for one reason or another, I was fully satisfied with. Also, I must confess my allergy to plugins in general, I prefer resorting to them as last option. So, solution rejected.

OK, the usual solution, try to fend for themselves. The first thing that came to my mind is going to give a look at the code of the function used by WP in its widget: wp_widget_rss_output. Browsing the code, they also use the fetch_feed function as seen before. This fundamental function, in turn, uses the most important feed parser written in PHP, SimplePie.

Wishing to output a function directly to a post or a page, first thing that come to mind are the shortcodes , a true wonder.

Fine, I decided: I would have customized the standard function wp_widget_rss_output. I would also have worked to get my function invoked through a shortcode.

My target, in short, was adding the feed to the post this way:

[add_rss url = "my feed uri" items = "number of items to retrieve" title = "title for" show_date = "show date or not" show_author "show author or not" show_summary = "whether to show a summary of each item"]

Warning! The following code goes to functions.php, a very critical file for your WP site. Double check every single line for missing parenthesis, if needed, or semicolon.

View the code on GitHub

First test

OK, time for testing. I opened a new post and typed:

[add_rss url="http://www.scoop.it/t/italiandirectory.review/rss.xml" items="4" show_author="1"]

The result is not particularly styled but got the point as you can see.

Houston, we have a problem

While I was uncorking the champagne, I wanted to test the real URL I had started this journey for, and typed in my post the following:

[add_rss url="http://italian-directory.it/feed" items="4" show_author="1"]

And the result was disappointing: why did my thumbnails fail to show?

Feed was embedded but no thumbnails

What’s the problem?

After several tests with the SimplePie enclosure object, that I initially blamed, I realised that was not a SimplePie quirk: the fact is WordPress looks like not caring to put thumbnails into feeds. To my knowledge it depends on your themes, sometimes they take care of feeding thumbnails sometimes do not.

After spending some more time on my tech resources (ok, just googling), I found some suggestions to force WP including thumbnails in your feeds, here is one. I did not test such suggestion but I may think it is a viable solution.

The point was I wished to code a solution working for any feed (with its thumbnails) not just for a site where I have admin powers. What about if I could scrape the featured image of every feed item and simply add it to my html? And that’s it.

Final solution: scraping each item’s featured image

Starting point is the PHP Simple HTML DOM parser that you have to upload to one of your folders, in my case the lib subfolder of my theme folder. Then, lines 110–125, I check for thumbnail link in the feed; if missing, I turn to scraping the featured image which usually has the “attachment-post-thumbnail” class.

And here is the final code. It checks whether your feed has thumbnail for every item; if not, the code goes for scraping the item featured image which, at least for WP feeds, has usually the attachment-post-thumbnail class. You can check the final result on this Hdemo page.

View the final code on GitHub

Originally published at www.italiandirectory.ru on November 14, 2015.

--

--

Hdemo Magazines Team
Hdemo Magazines

We are the Hdemo Magazines editorial team, a unique pool of copywriters and engineers to get you through technologies, science and culture.