Warning: Parameter 1 to wp_default_scripts() expected to be a reference, value given in /home/public/wp-includes/plugin.php on line 571
Fetch RSS Feed like Twitter & Google Reader from Wordpress

Fetch RSS Feed like Twitter & Google Reader from WordPress

// April 5th, 2011 // Posted in PHP // Tagged as , , , ,

Introduction

I want to share my Google Reader and put my tweets on this website. A quick search on Google reveals some plug-ins for WordPress but since I don’t feel like installing plug-ins I decided to implement it directly on the theme. That way I think it’s more faster and (for me) easier to maintain (as in I know my way around my own code).

Implementation

Apparently the implementation is straight forward. For this one i’m using WordPress 3.1 which uses SimplePie as its default RSS fetcher. The tricky part with SimplePie on WordPress is actually on how to refresh its cache. By default WordPress set the cache lifetime to 12 hours which is fine but I want to be able to manually refresh the cache. I’m not going to talk about it in here as I have covered the refresh issue in another journal, “Force Refresh SimplePie Cache on WordPress”.

Fetch Twitter: The Code

Here’s the code for fetching RSS using SimplePie:

$tweets = fetch_feed( 'http://twitter.com/statuses/user_timeline/pixelblender.rss' );
if( !is_wp_error( $tweets )){
    $items = $tweets->get_items( 0, $tweets->get_item_quantity(5));
    foreach( $items as $item ){
        echo 'title: '.$item->get_title().', permalink: '.$item->get_permalink().', date: '.$item->get_date();
    }
}

Here’s the breakdown:

$tweets = fetch_feed( 'http://twitter.com/statuses/user_timeline/pixelblender.rss' );

In this line using the built-in WordPress function fetch_feed we tried to get an RSS feed from a Twitter account. For this example I’m passing the URL to my Twitter feed. Simply replace “pixelblender” with your Twitter username. What happened is that the fetch_feed function will instantiate a new simplepie object for us to use. It will also setup all the needed cache settings etc. But you don’t have to worry about those details because fetch_feed simplifies it for us. If you want to go deeper you can take a look at the code in wp-includes/feed.php from the root of your WordPress installation directory. As for SimplePie you could read the documentation on their website.

The line if( !is_wp_error( $tweets )) is just a simple check to see whether fetch_feed was successfully executed or not.

$items = $tweets->get_items( 0, $tweets->get_item_quantity(5));

We use the get_items method on our simplepie object to limit the number of returned RSS “entries” that we want to use (in SimplePie the “entries” are represented as SimplePie_Item objects). You can read the documentation on get_items on SimplePie website. For this example i’m limiting it to 5 tweets only using the get_item_quantity method.

After that, the next step is to just simply go over each SimplePie_Item and process them. Method such as get_title, get_permalink and get_date are available. Here is the right time for you to format the output according to your layout. Again you might want to check the SimplePie documentation for more information.

Fetch Google Reader: The Code

For Google Reader it’s the same, you just have to change the feed URL:

$reads = fetch_feed( 'http://www.google.com/reader/public/atom/user/03240327085219016874/state/com.google/broadcast' );
if( !is_wp_error( $reads )){
    $items = $reads->get_items( 0, $reads->get_item_quantity(5));
    foreach( $items as $item ){
        echo 'title: '.$item->get_title().', permalink: '.$item->get_permalink().', date: '.$item->get_date();
    }
}

To find your Google Reader Feed URL: go to your Reader and from there look at the top left of the screen and look for a collapsable item called “All Items”. Underneath it you’ll find “Home”, “Starred items” and another collapsable called “Your Stuff” which contains “Shared item” and “Notes”. Click on “Shared items” which will take you to “Your shared items” page. On the top right side of that page you’ll see “show details”. Click on it to show details and it will shows your Feed URL.

Ads by Amazon.com:

Please share this journal with others:

2 Responses to “Fetch RSS Feed like Twitter & Google Reader from WordPress”

  1. SFR says:

    Hello,

    I am doing a design project and need some help with technical coding and language. I am trying to work in Processing, but since it is early in my project this can be changed if another program is more efficient for this cause.

    I found this article very interesting and informative, but I was wondering if you also have knowledge on how to collect specific tweets (in my case I want to collect all tweets, by anyone, that include certain words such as ‘no time’, ‘time money’, ‘spending time’ etc)
    Is this possible? The reason I am doing this is to put it onto an interactive webpage that will go on automatically with collecting tweets on these subjects.

    Thank you in advance and feel free to email me if that is better, then I can also send some codes of what I am doing now so you can better understand what I am looking for..

    SFR

  2. pietra says:

    using the same code you can fetch twitter search as well. replace the part that says:

    $tweets = fetch_feed( 'http://twitter.com/statuses/user_timeline/pixelblender.rss' );
    

    with

    http://search.twitter.com/search.atom?q=time%20money&rpp=15&page=1
    

Leave a Reply