Import and Display RSS Feeds in WordPress

by Jeff Starr on Sunday, April 26, 2009 100 Responses

[ ~{*}~ ] Importing and displaying external RSS feeds on your site is a great way to share your online activity with your visitors. If you are active on Flickr, Delicious, Twitter, or Tumblr, your visitors will enjoy staying current with your updates. Many social media sites provide exclusive feeds for user-generated content that may be imported and displayed on virtually any web page. In this article, you will learn three ways to import and display feed content on your WordPress-powered website — without installing yet another plugin.

On the menu for this tutorial:

  • Importing and displaying feeds with WordPress & Magpie (simple method)
  • Importing and displaying feeds with WordPress & Magpie (advanced method)
  • Importing and displaying feeds with SimplePie (WordPress not required)

Sound good? Let’s get to it..

Importing and displaying feeds with WordPress & Magpie (simple method)

If you only need to display the titles of a feed, you can take advantage of the built-in WordPress function, wp_rss(), which provides WordPress with essential feed-fetching and feed-parsing functionality. All you need to do is place the following code into the desired display location within your theme template file (e.g., sidebar.php):

<?php include_once(ABSPATH.WPINC.'/rss.php');
wp_rss('http://domain.tld/your-feed/', 7); ?>

In the first line, we include the required wp_rss() function from the rss.php file (rss-functions.php in older versions of WordPress). In the second line, we specify two parameters: the first is our feed URL and the second is the number of titles to display. Simply edit these two parameters and enjoy the results.

Importing and displaying feeds with WordPress & Magpie (advanced method)

Magpie provides an XML-based RSS built with PHP. WordPress uses Magpie to parse RSS and Atom feeds and display them on your website. Magpie parses feeds for two different WordPress functions:

  • wp_rss() - fetches and parses feeds for instant/automatic output
  • fetch_rss() - fetches and parses feeds for advanced/customized output

Each of these functions uses the Snoopy HTTP client for feed retreival, Magpie for feed parsing, and RSSCache for automatic feed caching.

The wp_rss() function fetches and parses feed content and then outputs an unordered list containing each of the feed’s post titles (see previous section). The fetch_rss() also fetches and parses feed content and will output the results according to your specific script configuration. Instead of just spitting out titles, the fetch_rss() function enables us to display any available feed data in customized format. Let’s look at an example.

<?php
include_once(ABSPATH.WPINC.'/rss.php'); // path to include script
$feed = fetch_rss('http://domain.tld/your-feed/'); // specify feed url
$items = array_slice($feed->items, 0, 7); // specify first and last item
?>

<?php if (!empty($items)) : ?>
<?php foreach ($items as $item) : ?>

<h2><a href="<?php echo $item['link']; ?>"><?php echo $item['title']; ?></a></h2>
<p><?php echo $item['description']; ?></p>

<?php endforeach; ?>
<?php endif; ?>

Once placed in the desired location in your WordPress theme template file, the previous code will output the title and description (content) of the seven most recent feed items. Simply edit the three variables in the first three lines of the script and you are good to go. Then, once you see that everything is working as intended, feel free to modify the markup as you see fit. Before moving on, let’s walk through the method in sequential fashion:

  • Include the script - specify the path to rss.php (or rss-functions.php in older versions of WordPress)
  • Specify your feed URL - specify the URL of the feed you would like to display
  • Limit number of items - edit the two numbers to reflect the numbers of the first and last feed items, respectively
  • Empty check - before running the loop, check that the feed isn’t empty
  • Begin the loop - begin the standard foreach loop
  • Display the items - for each feed item, display the post title, title link, and post content
  • Wrap it up - close the loop and end the empty check

Notes on using WordPress/Magpie
If you are calling feeds that may or may not include a description, you may want to avoid the output of empty paragraph elements ( <p> ) by wrapping the description with a conditional check like so:

<?php if (isset($item['description'])) : ?>
<p><?php echo $item['description']; ?></p>
<?php endif; ?>

Also, if the feed isn’t showing, try replacing the associated line with this:

$feed = @fetch_rss('http://domain.tld/your-feed/');

For an alternate way of checking for the presence of a working feed, replace the empty check with this:

<?php if (isset($rss->items) && 0 != count($rss->items)) : ?>

You can also clean up feed output by taking adavantage of WordPress’ built-in filtering functionality:

<?php echo wp_filter_kses($item['link']); ?>
<?php echo wp_specialchars($item['title']); ?>

Here are some of the variables that are available from your feeds (not sure if all of these work):

  • $item['link'] - post permalink
  • $item['title'] - title of the post
  • $item['pubdate'] - post publication date
  • $item['description'] - post excerpt or content
  • $item['creator'] - post author (does this work?)
  • $item['content'] - post content (does this work?)

Note that when using the $item['pubdate'] variable, the default output will look like this:

Mon, 11 Jul 2050 01:11:11 +0000

Fortunately we can clean this up a little bit by parsing it with PHP’s substr() function like so:

$pubdate = substr($item['pubdate'], 0, 16);

Which will output the following date format:

Mon, 11 Jul 2050

And, finally, an alternate loop format that defines the number of items to display without using the array_slice() function:

<?php for($i = 0; $i < 5; $i++) { $item = $rss->items[$i]; ?>
<!-- loop content -->
<?php } ?>

Importing and displaying feeds with SimplePie (WordPress not required)

For more robust feed fetching in the most simple way possible, SimplePie is the perfect choice. SimplePie is a blazing fast PHP class that is easy to learn and simple to use. With a few quick steps, you can use SimplePie to retrieve and parse any RSS or Atom feeds and display them on any PHP-enabled website (WordPress is not required). SimplePie works great with its default settings, but it is also highly customizable. You can use SimplePie to display any type of data from any number of feeds, and it even works without WordPress. Sound good? Here’s how to setup SimplePie in three easy steps:

  • Download SimplePie and unzip
  • Place the “simplepie.inc” into a folder named “php” located in the web-accessible root directory of your website
  • Create a folder named “cache” (also in the web-accessible root directory) and make sure that it’s writable by the server (i.e., CHMOD to 755, 775, or 777)

That’s all there is to it. Once SimplePie is properly setup on your server, you may check that it’s working by uploading the following code to the web page of your choice (edit as specified):

<?php // basic functionality check - edit script path and feed url
include_once $_SERVER['DOCUMENT_ROOT'].'/php/simplepie.inc'; // script path
$feed = new SimplePie('http://domain.tld/your-feed/'); // feed url

<h1><?php echo $feed->get_title(); ?></h1>
<p><?php echo $feed->get_description(); ?></p>

If the feed title and description don’t appear on the page, check the path to simplepie.inc in the first line and also verify that the feed URL is correct and that the feed is working properly.

Once everything is working and flowing like butter, the possibilities are endless. To get you started, let’s expand the previous “testing” code to include the title, link, content and date for the seven most recent feed items:

<?php 
include_once $_SERVER['DOCUMENT_ROOT'].'/php/simplepie.inc'; // path to include script

$feed = new SimplePie(); // bake a new pie
$feed->set_feed_url('http://domain.tld/your-feed/');  // specify feed url
$feed->set_cache_duration (999); // specify cache duration in seconds
$feed->handle_content_type(); // text/html utf-8 character encoding
$check = $feed->init(); // script initialization check
?>

<h1><a href="<?php echo $feed->get_permalink(); ?>"><?php echo $feed->get_title(); ?></a></h1>
<p><?php echo $feed->get_description(); ?></p>

<?php if ($check) : ?>
<?php foreach ($feed->get_items(0, 7) as $item) : ?>

<h2><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></h2>
<p><?php echo $item->get_description(); ?></p>
<p><small>Posted on <?php echo $item->get_date('j F Y @ g:i a'); ?></small></p>

<?php endforeach; ?>
<?php else : ?>

<h2>Feeds currently not available</h2>
<p>Please try again later</p>

<?php endif; ?>

That is the code I start with when implementing SimplePie on client projects. It serves as an easy-to-customize template that incorporates plenty of functionality and outputs feed content in common format. Let’s walk sequentially through the code for better understanding of functionality and proper use.

Setting the variables

  1. Path to the SimplePie script - edit according to your specific setup
  2. new SimplePie - setup a new instance of SimplePie feed variable
  3. Feed URL - specify the URL of the feed you would like to display
  4. Cache duration - specify how long the feed should be cached (seconds)
  5. Character encoding - ensures that content is in text/html, UTF-8 format
  6. Script check - sets a variable upon successful script initialization

Format the output

  1. Feed title and link - wrapped in <h1> tags and placed before the loop because only one title exists for each feed
  2. Feed description - wrapped up in paragraph tags and placed before the loop because only one description exists for each feed
  3. Initialization check - if the $check variable is set, then continue processing the script
  4. Begin the loop - standard foreach loop with parameterized argument for setting the number of feed items to display
  5. Feed items - for each feed item, display the post title, title link, post content, and post date.
  6. End the loop - terminate the recursive processing of feed items
  7. Else condition - if the $check variable is not set, then display the following code
  8. Else message - text/markup to display if the initialization check fails and the feed is not displayed
  9. End the if condition - conclude the script by terminating the if condition

Notes on SimplePie
It is possible to merge feeds into a single stream by replacing the associated lines with the following:

<?php 
$feed = new SimplePie();
$feed->set_feed_url(array(
	'http://domain.tld/your-feed-01/', 
	'http://domain.tld/your-feed-02/', 
	'http://domain.tld/your-feed-03/'
	));
?>

Also, you can truncate the number of words that appear in the content of each feed item by using PHP’s substr() function:

<?php echo substr($item->get_description(), 0, 180); ?>

In the foreach() loop, you may specify the number of the first and last feed to process by editing the “0” and “7” in the following line:

<?php foreach ($feed->get_items(0, 7) as $item) : ?>

For WordPress users who would rather not mess with all of the “under-the-hood” code stuff, there is an awesome SimplePie plugin that automates just about everything. It should be noted, however, that the SimplePie plugin consists of two different plugins and requires a significant amount of time and effort in order to implement. It’s a great set of plugins, but I would argue that the manual implementation method is easier.

Finally, keep in mind that your feeds are cached according to either the default setting or your specified cache duration. The default caching duration is one hour, which is suitable for most implementations. Don’t forget that, for caching to work, SimplePie must have write access to the cache directory (see above).

With SimplePie, the options are endless. This tutorial is enough to get you started, but you should check the SimplePie Wiki for more comprehensive documentation.

Feed ‘em!

With these versatile, easy-to-use techniques for importing and displaying external feeds, virtually anything is possible. If you are using WordPress, you can take advantage of the built-in Magpie functionality and display feeds quickly and easily. For non-WordPress users or for those seeking more control over the feed importation and display process, you will find all of the flexibility you need with SimplePie. These methods will help you provide more content to your readers and do so without getting locked into using yet another needless plugin. So go forth and feed that insatiable audience of yours! :)

About the author

[ Jeff Starr ]

Jeff Starr is a web developer, graphic designer and content producer with over 10 years of experience and a passion for quality and detail. Jeff is co-author of the book Digging into WordPress and strives to help people be the best they can be on the Web. + Follow Jeff on Twitter and subscribe to Perishable Press for quality web-design content delivered fresh.


100 Responses

Add a comment

[ Gravatar Icon ]

Peter Westwood#1

SimplePie is in trunk for WordPress 2.8

It would be good to update this to include what will be available then!

[ Gravatar Icon ]

Jeff Starr#2

Yes, I thought about waiting until 2.8, but figured I could just update the article when it is released. I am looking forward to SimplePie and WordPress working together — hopefully it will mean even more flexibility for designers and developers. Cheers!

[ Gravatar Icon ]

Frank#3

Nice post! I think it is better for the date you use the function of WordPress for formating the date with the options of the blog:
$pubdate = date_i18n(get_option('date_format'), $pubdate);
Best regards
Frank

[ Gravatar Icon ]

Jeff Starr#4

Excellent tip, Frank — thanks! :) The workaround mentioned in the post was a bit insufficient. This looks like it will work much better. Thanks for sharing!

[ Gravatar Icon ]

Frank#5

@Jeff: thanks for your answer; a small modification for my tipp:
$pubDate = date_i18n( get_option('date_format'), strtotime( $item['pubdate'] ) );
This works fine and the return is a date in the format of the blog-options.

[ Gravatar Icon ]

Jeff Starr#6

@Frank: Perfect! Thank you for the follow-up :)

[ Gravatar Icon ]

teddY#7

Wow, a really handy tutorial, Jeff! If it weren’t you writing this tutorial, I wouldn’t have found a way how to parse my Digg feed so that I can display it on my blog. I know that Wordpress has a function to import a RSS feed, but I have no idea how to process the information contained in it. Your guide saved me from all the need of Googling and piecing together snippets of code from all kinds of webpages, especially for the parsing of the time (pubdate). I have no clue how to change the way it is displayed until you wrote about it.

I’m considering to install a WP plugin called Lifestream to better manage all my social website activities (mainly Twitter, Digg and Flickr), and it seems to be a nice nifty plugin to try out. Of course, if it all fails, I can always use your failsafe method :)

[ Gravatar Icon ]

Thomas Scholz#8

Passing an URL to the SimplePie constructor triggers the automatic modus. Any later configuration attempts will fail. See: http://simplepie.org/wiki/reference/simplepie/start for details.

[ Gravatar Icon ]

Jeff Starr#9

@teddY: Always good to hear from you! Yes there are tons of great ways to use the feed-importing methods described in this article. There are many configurational options and different ways to use this type of functionality — not just with social media feeds, but with any other type of feed imaginable. I have several blogs to which I regularly publish content, and so having an easy way to import the various feeds makes it easy to display all of my content in one place.

The Lifestream plugin for WordPress looks pretty cool. I recently discovered Sweetcron, which is a robust and standalone (& self-hosted) lifestream application. It’s pretty cool — I setup a test site and have been playing around with it, but there is tons of configurational options and stuff you can do with it. I have seen some pretty sweet implementations around the Web. Maybe something to check out! :)

[ Gravatar Icon ]

Jeff Starr#10

@Thomas Scholz: The article has been updated with the correct information. Thanks!

[ Gravatar Icon ]

davis#11

Jeff,

Thanks for this! Works perfectly and easily!
I have, hopefully, a quick question for you though.

Using Wordpress/Magpie advance as listed above:

What about if i want to pass a variable for the URL….

Maybe an RSS feed URL listed in the User.

I have this to echo the user name(s) field value(s) (using a plugin to create a custom field in which i place the url) :

$values = get_cimyFieldValue(false, 'TWITTERFEEDURL');

       foreach ($values as $value) {
              $user_id = $value['user_id'];
              echo $value['user_login'];
              echo cimy_uef_sanitize_content($value['VALUE']);
       }

But if instead I wanted to pass the text value of that field into
$feed = fetch_rss('http://domain.tld/your-feed/');

How would I do that?
My attempts have proven my knowledge lacking ;)

[ Gravatar Icon ]

Jeff Starr#12

@davis: Ugh! I wish I could help but unfortunately that exceeds my limits of knowledge on the topic as well. Very soon, WordPress 2.8 will be released and will be using SimplePie instead of Magpie. Perhaps this will provide enable you to get the help you need via the actively moderated SimplePie help forum..?

[ Gravatar Icon ]

davis#13

ah, no worries Jeff.

I actually made it work making a variable equal to the function pulling the user field data (in this case being created by a plugin, but would work similarly for the “website” text field) - then i placed that variable into fetch_rss including all of that in a foreach after the data was retrieved and rss script included.

in my case (and in the event it helps someone else) the code is this:

include(ABSPATH.WPINC.'/rss.php'); // path to include script
$values = get_cimyFieldValue(false, 'FEEDURL');
foreach ($values as $value) {
       $user_id = $value['user_id'];
       $FV = cimy_uef_sanitize_content($value['VALUE']);
       $feed = fetch_rss($FV); // specify feed url
       $items = array_slice($feed->items, 0, 3); // specify first and last item
       echo "".$value['user_login']."";

              foreach ($items as $item) {
                     echo "<a>". $item['title']."</a>";
              }
              echo "";
}

now im off to try and filter the user role for which all of that is processed - wish me luck! haha.

thanks again Jeff!

[ Gravatar Icon ]

Jeff Starr#14

Good luck — and thanks for the followup with code solution. Very cool. I tried formatting it with some indentation and whatnot, so let me know if anything is incorrect.

Now I know who to contact if I ever need some feed-parsing ninja skillz! ;)

[ Gravatar Icon ]

Zeeshawn#15

Awesome article Jeff, Clear , to the point and so easy to follow. I’ll be disposing of my plugins and coding up my own code from now on. Love it mate.

[ Gravatar Icon ]

mccormicky#16

Sweet! Now if only I had had all this info last fall. I would have suffered a lot less…

[ Gravatar Icon ]

Jeff Starr#17

@Zeeshawn: Awesome, great to hear it — thanks for the feedback! :)

@mccormicky: I know what you mean. I should have written this article a year ago. I think it took me that long to understand it all myself. Oh well, at least it’s here for next time! :)

[ Gravatar Icon ]

Serge#18

Hi every one, you’ve done a great piece of code here.
I was fighting to get one of my own categories listed as RSS in separate page … in vane before i’ve come here.
I’ m barmen, not a code ninja…
Just a note about WP 2.7:

1 i couldnt include my own feed using your code (got a warning for the second display about function array …)

2 have given the feed to google feedburner and set the feed url of the last one

result: Worked as a swiss watches and fixed as well the problem of the “Frenchy letters” é l’e etc…

Thanx a lot for your being born.
the result is upgrading here : http://www.monmillion.fr/boutique/

[ Gravatar Icon ]

Mark#19

Man, I would love to get what @davis (comment #13) did working on my site but I am so far failing miserably. Help? Thanks!

[ Gravatar Icon ]

Jeff Starr#20

@Serge: I’m speechless! Thanks for the feedback :)

@Mark: I wish I could help here, but I am not familiar with the details of the technique. Hopefully davis will drop in again and lend a hand..

[ Gravatar Icon ]

davis#21

@mark

one thing to keep in mind before i break this down: the get_cimyFieldValue is a function created by the plugin Cimy User Extra Fields ( http://www.marcocimmino.net/cimy-wordpress-plugins/cimy-user-extra-fields/documentation/ )
I used it to create a field in the User profile. In this case a text field that i placed an RSS feed url into.

knowing that, here is the walk through.

include(ABSPATH.WPINC.'/rss.php'); — This is including the RSS magic

$values = get_cimyFieldValue(false, 'FEEDURL');
— This is saying the variable $values is equal to the function created by Cimy User Extra Fields false and ‘FEEDURL’ are options for that… in this case ‘FEEDURL’ is merely what i named the new text field i created with the plugin.

So it’s essentially just saying that I want $values to be the values of the new fields in the user profile, and Im calling to the new field ‘FEEDURL’ specifically - again that could be named anything.

foreach ($values as $value) {

Here it is saying run this for everyone of them

$FV = cimy_uef_sanitize_content($value['VALUE']);
This line i’ve created the variable $FV to be equal to another function created by the plugin (one to echo the text of the field i created) and called upon in the $values line above.

$feed = fetch_rss($FV); // specify feed url
—- Here im just telling it that $feed is equal to fetch_rss function with a feed value of the variable $FV which i created to call the function cimy_uef_sanitize_content which understands we are looking at the text field value for ‘FEEDURL’ per the $values = get_cimyFieldValues() line above.

$items = array_slice($feed-&gt;items, 0, 3); // specify first and last item
– This is styling the $feed

The rest is echoing to the page.
echo "".$value['user_login']."";
– This echos the user login name and below it is saying for each $item (that are pulled from $feed, that is fetching from $FV that is equal to cimy_uef_santize_content() that is limited from $values to ‘FEEDURL’) echo each feed entry as follows.

foreach ($items as $item) {
               echo "<a>". $item['title']."</a>";
}
echo "";

I hope that helps to explain what i did.
I still have to figure out how to only apply this whole thing to only one class of users (or to specific users) but i haven’t jumped back into yet to tackle that one.

[ Gravatar Icon ]

Patternhead#22

Great article Jeff.

I’m using WP 2.71. Does anyone happen to know if Magpie writes the cache to a table or to a file (I read somewhere that it adds entries to wp_options).

[ Gravatar Icon ]

Jeff Starr#23

@Patternhead: I think you are correct — there is no file that I know of that is used by Magpie and no information about setting permissions etc for such a file. If I remember correctly, there is a “magpie” field in the options table, as you suggest. If anyone has any specific information on this please share. Thx.

[ Gravatar Icon ]

westi#24

Yes WordPress does store the cache of the feed in the options table using a number of options (named magpie_x I think).

WordPress 2.8 feed functions, which now use SimplePie, will also cache in the options table but using the new transients functionality. What this does is store the information in the db unless you have installed an object cache plugin to use some thing like memcache for the WordPress object cache -then the transients are just stored in the cache.

[ Gravatar Icon ]

Patternhead#25

@Jeff @westi Thanks for the info. That’s interesting. So in terms of performance, I wonder which is quicker. Reading the cache from the DB or the disk.

I prefer to keep the DB as clean as poss so I’ve gone for simplepie (not the plugin) but this is a pretty big file 340kb so maybe it would be more efficient to just let WP use the DB for caching.

I’m planning to use 2.8 when we get RC1 so may have to take another look at this issue then. I haven’t use memcache before. Sounds like an idea for a future post Jeff ;)

[ Gravatar Icon ]

Jeff Starr#26

@westi: Thanks for posting! :)

@Patternhead: Yes, that is a great idea for a post! Looking forward to 2.8 :)

[ Gravatar Icon ]

Brian#27

Hi, Thanks for this code. I had it working correctly at one point but made the mistake of including small images in WordPress codes which prevented the RSS feed from working correctly.
However when I removed these I found that new items did not appear in the WordPress page although they do in the WordPress feeds.
Using phpMyAdmin I have looked at the database expecting to find a file (either the last one displayed on the page or the one after) with some peculiar setting but no.
I’ve deleted and re-entered all the affected bits but to no avail. The problem is displayed on

http://www.middlesbroughlourdes.co.uk/latest-news

where the sidebar links show the correct items but the body doesn’t.

Any ideas?

[ Gravatar Icon ]

Brian#28

Hi, Without doing anything on my part the problem described in my last post has disappeared.
Curious.
Once again, thanks for the code.

P.S. Just for the record I was using WP 2.8 on Safari 4 (Mac) and could see the exact same problem in iCab 4.6.0 and Camino 1.6.7.

[ Gravatar Icon ]

Jeff Starr#29

@Brian: Glad to hear you got it working — thanks for posting the follow-up comment stating that the issue has been resolved. Much appreciated :)

[ Gravatar Icon ]

Lekha#30

This is by far the best bit of information I have laid my eyes on!!!!!!!

kudos to you!!!

[ Gravatar Icon ]

Ray#31

Hey Jeff,

Great post as always!
Also great comments by the visitors.

Didn’t know that WP stores RSS cache in the DB. That doesn’t sound very nice.
I think I’m going to go with the manual SimplePie method.

Just adding my two cents, but probably the lightest RSS parser library is lastRSS. It’s also quite extendable, although not as full-featured as SimplePie.

By the way, you might want to update your tutorial for WP 2.8+ now ;)

[ Gravatar Icon ]

Josh#32

What if you wanted to include a custom field that is the url to the rss feed? I’m having difficulties since it’s php within php. Do I have to store as a variable first?

[ Gravatar Icon ]

Josh#33

I figured it out! You need to include global $post.

ID, 'rss', true);
wp_rss($rssfeed, 5);
?>

[ Gravatar Icon ]

Josh#34

Oops looks like it stripped the code. Let me try that again.

:���{
k����w�-

[ Gravatar Icon ]

Jeff Starr#35

Thanks for the follow-up post, Josh — glad to hear you got it working :)

(sorry for the code hassle — WordPress likes to gobble PHP!)

[ Gravatar Icon ]

Tim#36

How can we use the simple pie method described above now with WP 2.8?

[ Gravatar Icon ]

Jeff Starr#37

Hi Tim, as of WordPress 2.8, we can include and display feeds according to the SimplePie documentation. The only difference is that we use this:

<?php $feed = fetch_feed( $uri ); ?>

To return the feed as a standard SimplePie object. Everything else is according to the SimplePie documentation.

[ Gravatar Icon ]

Corey O#38

Is there any way to include an image while displaying the feed titles and description?

[ Gravatar Icon ]

Corey O#40

Thanks Jeff,

Nice site by the way. The background image is pretty awesome.

[ Gravatar Icon ]

John#41

Good bits, will pass them along!

Very useful site overall, thanks for sharing.

[ Gravatar Icon ]

WebDev.im#42

Very nice site. I will be trying to integrate your feed code into my site pronto.
Thanks for the resource.

[ Gravatar Icon ]

National Reporter#43

I was searching for a tool that would help me to feed articles for me to my website and i found it here and i think its very useful stuff , i need more closely look to include it in my wordpress, thanks for sharing and am going to use it in my blog right away.. Thank You.

[ Gravatar Icon ]

Nan#44

First of all, thank you so much for this code! :)

@Jeff @Franck: I try to change the default output of pubdate but unfortunately, neither of Jeff [$pubdate = substr($item['pubdate'], 0, 16);] and Franck [$pubDate = date_i18n( get_option('date_format'), strtotime( $item['pubdate'] ) );] work for me…

If I use Franck’s code, it is supposed to return the date [pubdate] in the format of the blog-options but it doesn’t. I set the date in the blog-options with 25/09/09 and I still get Fri, 25 Sep 2009 17:05:31 +0000.

Do you know why it doesn’t work for me? I restarted IE several times, emptied the cache but with no success… Thank you for your help! :)

[ Gravatar Icon ]

Flow Interactive#45

Hi, thanks for this great article, the best part being using the in built wordpress functions to cleanse the feed data and avoid nasty gremlins in the links. Really appreciate your time in sharing this.

[ Gravatar Icon ]

ILT#46

Hi there,

Fantastic feature! However I need to show the full text description, not just the excerpt. Can I achieve this using the method shown here, or is there a plugin out there that will do this for me?

[ Gravatar Icon ]

Jeff Starr#47

@ILT: I think the get_description function should display the entire description by default, but it’s been awhile since I have played with it. Also, if you are using the following function:

<?php echo substr($item->get_description(), 0, 180); ?>

You should be able to specify a significant large character value such that the descriptions will not be truncated.

[ Gravatar Icon ]

Recruitment SEO#48

Great - searched high and low for a plugin to do this with RSS and eventually found one but it makes miles more sense to use the RSS function already in WP - you’re a saviour.

[ Gravatar Icon ]

ILT#49

Thanks Jeff Starr :)

[ Gravatar Icon ]

Ron#50

thanks for the above RSS feed suggestions.

I have a quick question. Is there a way to screen incoming RSS feeds before publishing on a blog?

I would like to look at what is coming in before releasing it, similar to posts by contributors to one of our blogs that can be screened before being published.

Thanks

[ Gravatar Icon ]

davis#51

Ron
look at a plugin called Feed Wordpress http://projects.radgeek.com/feedwordpress

It allows you to pull a feed into a category which would then allow you to apply normal wordpress publishing rules to it.

[ Gravatar Icon ]

Ron#52

thanks I will check it out.

[ Gravatar Icon ]

trevorb#53

Hey,

Great tutorial even for me a ‘non-techie’ … i managed to make it work :)
One issue i have and im hoping you can help.

When my rss is displayed the the ‘title’ links correctly, however if the ‘description’ also contains a link, the link does not work? it links to my own blog?

Example:
i import a link like …

<a href="http://www. a site .com/subdomain/hello.html" rel="nofollow">hello</a>

how ever when this link displays in my blog the link looks something like

<a href="http://www. my blog.com/subdomain/hello.html" rel="nofollow">hello</a>

Can you show me an example how to fix this?

Thanks again!

[ Gravatar Icon ]

neel#54

Excellent article. I was searching for these kind of solution to display RSS from the RSS feed. Thanks for sharing it here.

[ Gravatar Icon ]

michael#55

Thanks much for this information, was wondering if you could help me out with something. I’ve implemented the first method and it’s pulling the data from an RSS feed just how I want, except I want the descriptions word count to be a bit shorter… do you know how I would change the word count?

I’ve tried a few things but can’t seem to decrease the word count.

Any help is much appreciated.

[ Gravatar Icon ]

michael#56

Well, I’m sure there’s another place within Wordpress that I can change the value, but I found a solution that does what I need.

I was actually using the second method listed on the page.

But now I’m just passing through $item['description'] through this function: http://vision-media.ca/resources/php/php-word-limit

However, I am curious to know if there’s a way to do this within wordpress so if anyone has any ideas I’d love to hear ‘em.

[ Gravatar Icon ]

Jeff Starr#57

Hi michael, I think I covered this in the article, just use this:

<?php echo substr($item->get_description(), 0, 180); ?>

You should be able to set 180 to whatever number of words you require.

[ Gravatar Icon ]

michael#58

Oh, I thought that was just for truncating by character and not by word. Will give it a go. Thanks!

[ Gravatar Icon ]

Jeff Starr#59

Ah, yes you are correct — substr() works at the character level; however, on the function’s PHP page, it looks like there are some techniques for counting words as well:

http://php.net/manual/en/function.substr.php

[ Gravatar Icon ]

umberto#60

very interensting and very usefull!

i have one question…i’m fletching the feed by yahoo pipes, and works fine, but how can I get the link to the site from which each feed? I tryed with some simplepie function but it doesn’t work…

there is my code:

<?php
     include_once(ABSPATH . WPINC . '/feed.php');
     $rss = fetch_feed('http://pipes.yahoo.com/pipes/pipe.run?_id=64b9e984517872578eeb5ed99ed6b69c&_render=rss');
     if(!empty($rss)):
     $maxitems = $rss->get_item_quantity(5);
     $rss_items = $rss->get_items(0, $maxitems);
     endif;
     ?>
     <ul class="feed">
          <?php if ($maxitems == 0) echo '<li>No news.</li>';
          else
          foreach ( $rss_items as $item ) : ?>
          <li>
          <?php echo substr($item->get_description(), 0, 180); ?>
          <a href="<?php echo $item->get_permalink(); ?>" rel="bookmark" target="_blank"><?php echo $item->get_title(); ?></a>
          </li>
          <?php endforeach; ?>
     </ul>

i’m going mad for obtain something like:
title of the external post - via name of the site

have u any suggestion?
thanks a lot and sorry for my bad english!!!:)

[ Gravatar Icon ]

nishant#61

I need to display some news feed. at my site where I want to display the
news, I want one image also with the news title and description. (I mean
image fr each item in rss element) how can I do that. I think the news feed
doesnt give me any image.

Is there something like I can read the link and get the image and display at
my site.

[ Gravatar Icon ]

Stefan Segers#62

Hey guys,

I finally got everything working except for the pubdate. I also tried both options but non is working.

I edited the time/date settings on origininal website (where the feed is comming from) and on the website where I want to show the feed.

None is working.

I really hope there will be a good solution soon.

Stefan

[ Gravatar Icon ]

Wibbler#63

This is excellent. Thank you for your advice - RSS parsing in Wordpress is never easy…

[ Gravatar Icon ]

Norbert#64

Hi nishant,
did you already succeed to reed out an image from a feed?
I also wonder if would be possible to read out (on my blog) a customfield value that the post autor gave to the post?
i mean… i know how to handle custom fields and thanks to this post it’s clear how to get headlines from other blogs, but how can i get a custom field value along with the headline? …or is it possible at all???

[ Gravatar Icon ]

cooljaz124#65

Jeff , this is wonderful and clearly explained. Im here through Diggin into wordpress book :)

[ Gravatar Icon ]

Jeff Starr#66

@cooljaz124: Great to hear it - and welcome to Perishable Press! :)

[ Gravatar Icon ]

Matt#67

Thank you thank you thank you

[ Gravatar Icon ]

Kathy P#68

Hey there,

In order to extract the creator’s name you would have to put rather than . I found this article very helpful, so I thought it would be helpful to amend it with some more useful tips. I had to search all over the Net to find solutions for exporting external RSS feeds. Thanks for the article!

KP

[ Gravatar Icon ]

Kathy P#69

The last post did not display the code correctly…
* <?php echo $item['dc']['creator']; ?> rather than <?php echo $item['creator]; ?>.

[ Gravatar Icon ]

Lara#70

Why won’t this parse this feed?
http://www.facebook.com/feeds/page.php?format=atom10&id=344692830875

Arghhhh, it works with any other feed (such as a FB profile) but not a FB fan page!

I’ve trouble shooted quite a bit. Any info?

[ Gravatar Icon ]

Jeff Starr#71

@Lara: Try running that feed through Feedburner, if possible, before importing it to your site. The issue may be formatting-related and Feedburner is great at cleaning things up to work just about anywhere.

[ Gravatar Icon ]

Ann#72

For importintg and displaying RSS, I just the Simplepie plugin for Wordpress. It sets up easily and works every time for me.

[ Gravatar Icon ]

Pam#73

So I have this working and pulling in my RSS, but it is coming in at approx. a 14pt font. How do I get it to use my CSS?

Also, has anyone ever pulled in an outside RSS feed and merged it with a current blog? My client has hired someone new and wishes to integrate their existing blog with the outside RSS feed of the new hire’s blog. The new hire is comfortable posting to his existing blog, so they don’t want to disturb the flow. Any help is appreciated, otherwise they must keep the posts separated by author.

Thanks.

[ Gravatar Icon ]

Jeff Starr#74

Pam, if you are importing the feed, it will inherit whichever styles are applied to the HTML page that includes it. And for the other thing, I am sure there are several plugins that do that exact thing.

[ Gravatar Icon ]

Lost Steak#75

$item[’content’] - post content (does this work?)

No it doesnlt

The content comes across as an array

So just mash it to gether with the implode command

items, 0, 7); // specify first and last item
?>

<a href=”">

Hope this helps
-Tom

[ Gravatar Icon ]

Lost Steak#76

$item[’content’] - post content (does this work?)

No it doesnlt

The content comes across as an array

So just mash it to gether with the implode command

-> include_once(ABSPATH.WPINC.’/rss.php’); // path to include script
-> $feed = fetch_rss(’http://best-muscle-gain.com/feed/’); // specify feed url
-> $items = array_slice($feed->items, 0, 7); // specify first and last item
-> ?>
->
->
->
->
-> <a href=”"> ?>
->
-> $myarray = $item[’content’];
-> $mystring = implode(” “, $myarray);
-> echo “$mystring”;
-> ?>
->
->
->

Hope this helps
-Tom (Take 2)

[ Gravatar Icon ]

Jeff Starr#77

Please wrap lines of code with <code> tags (prevents WordPress from gobbling stuff). Thanks!

[ Gravatar Icon ]

Romul#78

I’ve used the second method. It works perfect.
Thank you!

[ Gravatar Icon ]

Fred#79

Hello i am trying to include an external rss feed into a wordpress article with the picture attached with each title of the feed i want to display. How to do That ? Thans

[ Gravatar Icon ]

Jeff B#80

Anyone know how to get the links to open in a new window?

I tried target="_blank" in different places but it’s not working. Probably because this code is php.

[ Gravatar Icon ]

Wawa~*#81

hey, thnks for sharing… it works perfectly fine on my site ;)

[ Gravatar Icon ]

Thumb and Hammer#82

Thank-you very much for this post. I had been searching for a while for information on how to display the latest posts from my BBPress forum on my home page but kept coming up empty until I found your example using SimplePie. Thanks again.

[ Gravatar Icon ]

Raja MM#83

Really a great post…

One suggestion or need an idea,

fetch_rss('http://blogsearch.google.com/blogsearch/feeds?bc_lang=en&amp;hl=en&amp;output=atom');

it doesn’t fetches description of atom feed. any idea?

[ Gravatar Icon ]

Stephen G#84

Hey Guys,

I have had a feed converted from xml to rss and the date time stamp is not perfect. But…it takes a long time to load the rss, too long for wp-O-matic and feedwordpress to add the feed. Is there a way I can modify a script so it takes longer to get the feed?

I think they use simple pie and magpie?

Any way a bit over my head - just a pain as I had this RSS feed built specifically to add into wordpress. I am unable to pass a date parrameter to it to just show today.

[ Gravatar Icon ]

Virtualization#85

Hi,

Thank you for an informative article.

Do you know if there is an RSS plugin for wordpress (or standalone) which allows parsing of rss feed (images) into multiple columns?

Usually, when you parse pictures/gallery feed, parser displays the pics in one straight column/line down. It would be nice to break that up into 3,4 or 5 columns and rows for embedding onto pages/posts.

Thanks

[ Gravatar Icon ]

Jeff Starr#86

@Virtualization: You may have some luck styling your feeds with CSS, and I recall seeing one or two decent plugins that allow you to do this. You will need a custom feed template if you need more control (e.g., over the markup) than CSS. Remember though, feeds are generally delivered to a wide variety of feed-readers and devices. If you modify the structure/style too much, users may experience display/format issues with their preferred readers.

Trackbacks / Pingbacks
  1. Lenguajes X » Importar RSS en WordPress sin usar un plugin
  2. Importar RSS en WordPress sin usar un plugin | Blog Ascariz
  3. Enlaces de la semana (III) | Mareos de un geek
  4. links for 2009-05-27 « Rob Parker – robparker.org.uk
  5. » Importar RSS en WordPress
  6. WordPress/WordPress MU: RSS On A WordPress Page | Richard Bui | Bui4Ever.com
  7. Knowledge Base
  8. Come (ma Soprattutto Perché) Implementare un Sideblog - seconda parte » Francesco Gavello - Blog Marketing Tips, Web & Blogosfera
  9. online backup programs software technical comparison | yon Leveron blog
  10. Twitter Trackbacks for How to Import and Display RSS Feeds in WordPress • Perishable Press [perishablepress.com] on Topsy.com
  11. How to Import and Display Feeds in WordPress 2.8 and Beyond | Digging into WordPress
  12. Google Goggles, RSS and WP, Wordpress Hacks, Private Communities @ Webmaster Chronic Blog
  13. blogtest » Blog Archive » Importing feeds
  14. WordPress Plugin Support Aggregator | Michael Fields: WordPress Development
Share your thoughts..

Read Comment Policy

Comment Rules: No spam. No profanity. Use your real name. You may use simple HTML tags for style. Wrap all code in <code> tags. Learn more.



Attention: Do NOT follow this link!