Display RSS Feed in WordPress
This article was written in 2011 👴🏼. Most of the tips here are not valid anymore and I don't recommend using any of the snippets displayed here.
I'm keeping this article here for historical reasons and to remind myself how much I've improved over the years.
Only cosmetic changes were made to this article.
One of the things I love about WordPress is being able to do pretty much anything, and its built-in functions make this even easier. A few days ago I had the need to create a template to fetch and display posts from a feed. Despite being my first time doing something like this I got it working in a very short time. even more, I found a couple of ways to do this.
Option 1: WP_Http
The person who asked me to do the template left me a link to Ozh's
blog, where he explains how to use the WP_Http
class to fetch content from external
sites, a simple, well explained way. Once again, there's more than one way to do this.
Using the class
The basic form (which Ozh explains in his article), is done by making a call to the class itself.
First we include the class (since WP 3.x)
1if( !class_exists( 'WP_Http' ) )2 include_once( ABSPATH . WPINC. '/class-http.php' );3
Then we make the call and test the conection:
1$request = new WP_Http;2 $result = $request->request( 'http://feed.url' );3
If succesful, then $result
will be an array with the following data:
'headers'
: Array with… headers… like"x-powered-by" => "PHP/5.2.1"
'body'
: The feeds content, the thing we need. Returns Raw HTML'response'
: Array with the server's response, we needarray('code'=>200, 'message'=>'OK')
'cookies'
: Array with cookies info.
Now the good stuff, the implementation:
1function fetchFeed2($strFeedURL) { // Feed function2 if( !class_exists( 'WP_Http' ) ) // WP_Http Class3 include_once( ABSPATH . WPINC. '/class-http.php' ); // Include class45 $request = new WP_Http;6 $result = $request->request( $strFeedURL ); // test conection78 if ( ! is_wp_error($result) ) { // No error9 if ( ( $result['response']['code'] = 200 ) && ( $result['response']['message'] = "OK" ) ) {10 return $result['body']; // get data11 } else {12 return $result = "";13 }14 } else { // error15 return $result = "";16 $error_string = $result->get_error_message(); // which error was it?17 echo '18' . $error_string . '; // display error19 }20}21
Now we just have to add this where we want the feed to display.
1<?php2 $strFeedURL = "http://feed.url";3 echo fetchFeed2($strFeedURL);4?>5
Easy right? source
Using functions
As I mentioned before, there's more than one way to use this class, the other way
is to use the functions (instead of the class itself), in this case wp_remote_get()
.
We could say this is the fastest way to use the class, we just need this code:
1$response = wp_remote_get( 'http://feed.url' );2if( is_wp_error( $response ) ) { // error3 echo 'Something went wrong!';4 $error_string = $response->get_error_message(); // which error was it?5 echo $error_string; // display error6} else { // No error7 echo $response['body']; // Display content8}9
The problem here is that (at least in my case) it returned the content in XML, and I'm not really good with that, how to turn it into simple html?
Option 2: Simplepie
Mi ultimate solution was to use other method, luckily we have Simplepie already
integrated to WordPress, now we just have to call the function fetch_feed()
.
1<?php2$rss = fetch_feed('http://feed.url');3if (!is_wp_error( $rss ) ) : // Was the object created?4 // How many items we got? (Max 5)5 $maxitems = $rss->get_item_quantity(5);6 // Create an array with items starting with #07 $rss_items = $rss->get_items(0, $maxitems);8 endif;9?>10
Let's show the items, just like a WordPress loop
1<ul>2<?php if ($maxitems == 0) echo '<li>No items.</li>';3else4// Loop through each feed item and display each item as a hyperlink.5foreach ( $rss_items as $item ) : ?>6<li>7 <a href='<?php echo esc_url( $item->get_permalink() ); ?>'8 title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'>9 <?php echo esc_html( $item->get_title() ); ?></a>10</li>11<?php endforeach; ?>12</ul>13
This was with no doubts the easiest and fastest way since it allows me to customize almost entirely the way the items are displayed, the use is very similar to the WordPress loop and it supports a number of tags, every one listed in Simplepie's documentation.