[Shortcode] Include an RSS feed in a post or page with shortcode

One of the most common questions I’m asked is how to include an RSS feed into a post or page. Well, with WordPress shortcode and the built in rss.php functionality, it’s easy!

WordPress won’t natively allow you to enter an RSS feed into a post or page but this shortcode function will allow it. If you were savvy enough, you could extract audio from the RSS feed and have a podcast play within a post or page as well. This is something I’ll probably post later.

Open up your theme’s functions.php file and enter the following code.

First, you will need to include the WordPress rss.php file to use the wp_rss() function.

include_once(ABSPATH.WPINC.'/rss.php');

Now, directly below it, add the following code.

function display_rss($atts) {
extract(shortcode_atts(array(
   "feed" => 'http://',
   "num" => '1',
   ), $atts));
return wp_rss($feed, $num);
}
add_shortcode('rss', 'display_rss');

Now, whenever you want a feed to display you’ll do in the following manner:

[rss feed=”http://www.flightpodcast.com/feed” num=”5″]

The feed is the URL of the RSS feed you’re including and num is the number of posts that you would like to return.

As a side note, if you wanted to include an RSS feed in a caveman manner by integrating it into one of your theme’s files, you would use the following method.

<?php include_once(ABSPATH . WPINC . '/rss.php');
wp_rss('http://www.flightpodcast.com/feed', 5); ?>

Having provided that, don’t bother. Using shortcode is bulletproof and provides a global function you can use anywhere.

Shortcode has bridged the gap between the so-called ‘superior’ functionality of Thesis (which does have certain advantages) and the core WordPress intallation.

First Name:
Your Email Address:
 


If you liked this article, you may also like:

  1. Add author details after each post in a WP RSS feed
  2. A Better Way to Add RSS Feeds to Your WordPress Blog (Using Shortcode)
  3. Exclude posts from WordPress blog or RSS feed based on a tag
  4. [Shortcode] Examples [1]
  5. Add Your Latest Google Plus Post(s) to Your WordPress Blog (or Generate a Google+ RSS Feed)
About Marty

is a passionate web developer from Sydney, Australia. He owns about 600 websites and makes a healthy living from working the web. As a day job, he works as a pilot for an international airline. Follow Marty on Twitter or Google+.

Comments

  1. Ramona says:

    I use a shortcode.php that I embed into the functions.php. The code include_once(ABSPATH.WPINC.’/rss.php’); is part of shortcodes.php or functions.php? I’m not sure.

    • Marty says:

      Hi. So you have include(“shortcodes.php”); in your functions.php file? That’s a good method. It shouldn’t really matter where it goes because it’s referencing the path to rss.php. If all else failed, you could simply provide a full path.

      WordPress uses the Snoopy Class to retrieve the feed and, more importantly, Magpie to parse it. As such, you have far more functionality than described above (some plugins take advantage of this and do some pretty cool stuff). It’s probably about time I improved upon the above (which is basically there to illustrate a basic point). It doesn’t do feed error checking nor does it parse the feed for more useful information – such as descriptions. I’ll add additional shortcode soon.

      You have my email now. Feel free to send me what you have done if you get stuck.

  2. Ramona says:

    At the moment I use a plugin of Frank Bültge (WordPress Germany): http://bueltge.de/wp-rss-import-plugin/55/. Here you can import feeds within posts and pages. I will keep your solution in mind.

  3. TSB says:

    Hi, thanks for this code. I’ve implemented it and the shortcode successfully displays the rss feed. The problem I have is that it outputs the RSS feed at the top of the page, regardless of where I place it in the post/page. Any thoughts?

    Thanks!

    • Marty says:

      Hi. I don’t want to neglect your problem, but it may be easier for you to try this code (A Better Way to Add RSS Feeds to Your WordPress Blog [Using Shortcode]) instead. It uses the up-to-date WP library and is far more robust.

      Usually, the problem with shortcode at the top of the page is due to ‘echo’ing the returned shortcode… but that won’t be the problem if you’ve copied the code above.

      If you have a chance to check out the other code, it may solve your problem. Please let me know how it goes before I have a play with the code myself.

  4. TSB says:

    Very interesting. That new code is doing the trick. Odd that the old code was having that problem. Thanks!

    • Marty says:

      Good to hear. The wp_rss function is long depreciated (replaced with fetch_feed) so I suspect it’s simply no longer supported in any way… I’ll look into it. The new code is far better, anyhow. Good luck with it!

Speak Your Mind

*