Get $500+ of the best After Effects files, video templates and music for only $20!
Add the WordPress RSS Feed Anywhere In Your Theme

Add the WordPress RSS Feed Anywhere In Your Theme

Tutorial Details
  • Program: WordPress
  • Version (if applicable):3.0 and up
  • Difficulty: Easy
  • Estimated Completion Time: 15 minutes

If you’re looking for a way to display RSS feeds anywhere on your site and you’re using WordPress then you’ll be happy to hear about this trick. You know about the WordPress RSS widget… but what if displaying a feed in the sidebar is not enough. This tutorial will show you how to take an RSS feed and put it anywhere in your theme, including a page or post template. This is a great solution for you folks out there using RSS feed generators (say goodbye to small footer credits) or plugins (say hello to a faster website)!


The Code

Let’s start this by dishing out the code, then we’ll break it down step by step. This is the code that you’ll be placing into your theme template where you’d like the feed to show up. For instance, you could place this into a custom page template for a dedicated, styled RSS feed ;)

Part 1

<?php if(function_exists('fetch_feed')) {

	include_once(ABSPATH . WPINC . '/feed.php'); // the file to rss feed generator
	$feed = fetch_feed('http://www.brettthompsonracing.com/feed/'); // specify the rss feed

	$limit = $feed->get_item_quantity(7); // specify number of items
	$items = $feed->get_items(0, $limit); // create an array of items

}
if ($limit == 0) echo '<div>The feed is either empty or unavailable.</div>';
else foreach ($items as $item) : ?>

// The actual output
<h1><a href="<?php echo $item->get_permalink(); ?>" alt="<?php echo $item->get_title(); ?>"><?php echo $item->get_title(); ?></a></h1>
<p><?php echo $item->get_date('j F Y @ g:i a'); ?></p>
<p><?php echo substr($item->get_description(), 0, 200); ?> ...</p>

<?php endforeach; ?>

The Breakdown: Part 1

The idea is to first find the feed generator include_once(ABSPATH . WPINC . ‘/feed.php’); which is needed to be in the correct location in order for this code to work.

Then you add your rss feed $feed = fetch_feed(‘http://www.brettthompsonracing.com/feed/’); that you want to display on your site. Some feeds require the / character at the end. Keep in mind not all rss feeds will work; if that is the case you can always turn the feed into a feedburner RSS feed.

$limit = $feed->get_item_quantity(7); will determine how many posts you’d like to show. Replace 7 with whatever number fits your needs.

If there is no posts available then it will display a error message “The feed is either empty or unavailable.” Replace the text with whatever wording is appropriate. If you get this message then your code is most likely working.

    Here are your options you can display from the feed. You’ll notice thumbnails are not available. Whatever you decide will be repeated if you have multiple feed posts.

  • get_title(); ?> = Post Title
  • get_date(‘j F Y @ g:i a’); ?> = Date, if you want to display the date a different way go here
  • get_permalink(); ?> = Link to Story
  • get_description(), 0, 200); ?> = Excerpt or Description, change 200 to whatever amount of characters you would like to display

Part 2

The default for your RSS feed refreshes with new posts every 12 hrs. This code will cause the feed to be checked every 30 mins (place this code in your functions.php or custom_functions.php theme file):

add_filter( 'wp_feed_cache_transient_lifetime', create_function('$rssfix', 'return 1800;') );

The Breakdown: Part 2

add_filter is a WordPress function call that allows you to ‘hook’ into the WordPress core and execute a function during a certain operation.

The hook is wp_feed_cache_transient_lifetime. It’s the hook that handles the feed refreshes.

Then comes create_function(‘$rssfix’, ‘return 1800;’) which sets a time to check the feed more promptly. The code is set to 30 minutes, so if you’d like a different time then change the 1800 to your desirable time. 600 = 10mins, 1200 = 20mins, 1800 = 30mins. $rssfix can be changed to whatever text you’d like, but remember to keep the $.

There you have it. Take this idea and put it in a php widget, theme, post or page template. As I mentioned at the top of the post, you could place this into a custom page template for a dedicated, styled RSS feed.


Editors Note! This article originally ended in a few final tips… a few final tips that suggested that you open up a file inside the WordPress core and make changes. These changes were beneficial overall and very well intentioned on the surface, but it needs to be said that the WordPress core code is sacrosanct, and should never, everever be changed. Can you do it? Technically, yes… but we’ve officially decided that this site won’t go near the topic with a ten foot pole as it represents an undermining of some of the principles that makes WordPress great (a secure core, safe updates, oh, and not needing to ever touch the core to get what you want). Thanks for all of the comments – they help make the site better and they’re all appreciated!

Add Comment

Discussion 17 Comments

  1. curtismchale says:

    I’m more than a bit disappointed that you recommend hacking core to remove the rss icon in the default widget. You should never hack core, it’s simply going to be overwritten next time you upgrade the site. You should be showing someone how to build their own widget that looks like the core one but omits that icon. CSS is the way to go here the other option shouldnt even be mentioned.

  2. Craig Morgan says:
    Author

    Curtis does bring up a good point. To be on the safe side you want to use the css style display:none, because it hides an element. The element will be hidden, and the page will be displayed as the element is not there. Personally I don’t like hiding code, because it adds to the code which adds to the file size which slows down your website. Now you need a heck of a lot of hidden code to slow down your website, but I’m just saying. Options are good Curtis, options are good.

    In conclusion, only delete the code shown above in the file wp-includes/default-widgets.php if you plan on not upgrading wordpress to the newest install when available.

    • curtismchale says:

      While it might be possible for an experienced developer to watch the updates and not get their edit overwritten (especially with delta updates now in WordPress) many of the readers are not strong developers and will have no idea the full consequences of ‘hacking core’.

      I had hoped that Envato/Tutsplus would have a stronger editorial ethic that would include never allowing ‘hack core’ to get in to their tutorials, since it’s considered extremely bad practice.

    • Piet says:

      Like the others I find it unbelievable that a site like yours even offers hacking the core code!

      And then you even make it worse by offering the “suggestion” not to upgrade to the newest WordPress install when available?

      Are you serious???

      TutsPlus should add a sentence under each article that “these views are those of the author only and do not necessarily represent those of the site in general”, because I cannot imagine they stand behind what you have just written, Craig!

      The rest of the article offers a good tip, but cannot like or plus or promote it in any other way anymore…

      • Brandon Jones says:

        Howdy Piet! Thanks for the note – Craig mentioned that it wasn’t really a kosher practice in the offending section, but I’ve just gone ahead and removed it to be safe. Editing the WordPress core code is never a good idea – using filters and hooks to do stuff like that is fine, but the core code really should be sacrosanct ;)

  3. This is really irresponsible. The publisher should be ashamed of themselves for running it.

  4. Andrew Nacin says:

    I thought these tutorials go through an editing process?

    • Brandon Jones says:

      Heya Andrew – they do ;) I usually allow for a little bit of “best practices” leeway with code as I prefer to allow individual authors speak their minds. In this case I let the final section slide with a “don’t do this, but…” comment, which was seriously overly lax in my case. I’ve since pulled the last section which mentioned touching the core code, and this will simply become a lesson for the future (among other lessons that we’ve been learning along the way). Thanks for the comment, it’s honestly appreciated!

  5. Editing anything in WordPress’ core code is not remotely advisable for the simple fact that your client, at some future date, will more than likely click that “Upgrade” link to automatically upgrade WordPress & wipe out any edits you’ve advised their developer to make to core.

    You’d be better off illustrating how to repurpose the default rss widget code into one’s theme.

    • Brandon Jones says:

      Thanks for the note Pat – changing core code is always, always ill advised, even with the “don’t do this line” – but I’ve gone ahead and just removed the final section to be safe. ;)

  6. Vivek Parmar says:

    if such a good site teaches you how to hack a core, then I do not think that hackers have to work hard…
    Really these types of articles need editing…
    A simple widget or a plugin would be the great idea..

  7. Gary Walsh says:

    Many thanks for this Craig, however there is a typo in Part 1 of the above code on line 14 giving rise to a parse error.

    Working version below.

    get_item_quantity(7); // specify number of items
    $items = $feed->get_items(0, $limit); // create an array of items

    }
    if ($limit == 0) echo ‘The feed is either empty or unavailable.’;
    else foreach ($items as $item) : ?>

    <a href="get_permalink(); ?>” alt=”get_title(); ?>”>get_title(); ?>

    get_date(‘j F Y @ g:i a’); ?>
    get_description(), 0, 200); ?> …

  8. Gary Walsh says:

    Given the remit of this site, it’s frustrating the comment form cannot better process code examples. I’ll try again, but isolate the error and keep things simple.

    Getting a parse error? Here’s the solution:
    <?php echo $item->get_title(); ?>
    Typo correction for the example code in Part 1, line 14.

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.