Please disable ad blocker to see the page.

Simple Code To Fetch RSS Feeds For Your WordPress Website

Here I am going to tell you about RSS feeds for WordPress. If you like to get updates of your favorite sites or want to show content of any site in WordPress, you don't need to visit the site. RSS feeds is a great way to do this task. It also helps to enrich the primary content.


RSS fetch the content automatically from the external source. It is an XML document with the details like published date, author name, images (if any), etc.

WordPress has in-built Magpie engine to fetch RSS feeds and display it on your website. In this article you'll see just two simple methods to fetch and display feeds.

First - By Widget:

WordPress has an existing widget for RSS. If you want to display feeds anywhere in sidebars. Follow the below steps:

Go to the Dashboard -> Appearance -> Widgets


Look for RSS in available widgets list.


Simply drag it to that sidebar area where you need to display feeds.


Then it will show you options list to configure.


Enter the url like this http://website.com/?feed=rss2 in the first textbox and any title in the second.

Set the limit from drop-down.

With checkbox, keep control on the content to be displayed.

Click on save button.

You have completed.

Open the front-end you can see the RSS feeds on the sidebar.

In-built Functionality

Following are the two in-built functions are available in WordPress. Both are used to fetch RSS feeds but for different versions.

1. fetch_rss($uri) - Use this function if WordPress is older than 3.0.
2. fetch_feed( $uri ) - for latest version.

Now set eye on the explanation.

Firstly I will write it for older version:

File inclusion is required because functions are defined in the file.
include_once(ABSPATH . WPINC . '/rss.php');

Then call the function as below. It fetches and parses the feeds to display the data.
$rss = fetch_rss('url');

Extract the given number of element from an array.
$maxitems = 5;
$items = array_slice($rss->items, 0, $maxitems);

Here we will retrieve each element one by one.
foreach ( $items as $item ):

Show the title.
$item['title'];

And the full or summarize data.
$item['description'];
endforeach;

For newer version:

At first, include the file.
include_once(ABSPATH . WPINC . '/feed.php');

Use the below function.
$feeds= fetch_feed('url');

Feeds to display.
$maxfeeds = 5;
$feeditems = $feeds->get_items( 0, $maxfeeds );
foreach ( $feeditems as $item ):
$item->get_title();
$item->get_description();
endforeach;

Note: place the code inside php tag.

As shown already there is no big difference in both version. rss.php or feed.php reside in wp-include folder, if rss.php is available here than you may also use fetch_rss in latest version but not recommended.

I have written RSS data in effortless way, you can customize the display of data according to your theme. Either create new page template or use existing to show the data with the above code. It is a best approach to fetch RSS feeds for WordPress without wasting time to search for plugins because WordPress itself provide you the facility to accomplish the goal.
Previous
Next Post »
1 Comment
avatar

Any idea on how to get the current fetch_feed() to stop stripping html tags? or filter them? In particular I want to pull in an RSS feed with YouTube iframes in the content but they keep getting removed.

Balas