WordPress Google Reader Widget

Number 3 in Widget-A-Day is a Google Reader widget.

This widget will display your latest shared stories. You can change the amount of stories to display and how to display them.

Download googlereader.zip

Default display:
googreader1cropped.png

Admin panel:
googreader2cropped.png

Download googlereader.zip

SVN Checkout
svn co http://svn.wp-plugins.org/google-reader-widget/trunk

Requirements

Installation

  1. Download googlereader.zip
  2. Extract and upload googlereader.php to the plugins/ directory
  3. Enable Google Reader in the Plugin admin panel
  4. In widget admin panel, place Google Reader in the sidebar, and edit it to enter your user id

Features

  • Displays your latest shared items from Google Reader
  • Completely customizable display
  • Caching for large traffic sites

User ID

In order to get your shares, you need to know your user id according to Google Reader. This can be found by going to the ‘Shared Items’ link, and copying the 20 digit number at the end of your shared items link.

So something like:
http://www.google.com/reader/shared/02774557510273097991
Where 02774557510273097991 would be my user id.

Formatting

There are 3 parts needed to format the output.

The first part, called items start in the admin panel, is the first part of the widget after the title. For the default formatting, this is just <ul>.

The second part, called items end, is the ending of the widget. By default, this is:
</ul>
<a href="%googlereader%" style="float:right;">Shared Items</a>

The third part is what is called for each item. By default, this is:
<li style="list-style-type: none;"><a href="%link%">%title%</a></li>

The premise of calling each value is this:

  1. start
  2. item
  3. item
  4. item
  5. end

The formatting for items is:

  • %link% – Link to the item
  • %title% – Title of the item
  • %site% – The title of the site the item is from
  • %sitelink% – The link to the site the item is from
  • %number% – The number of the current item
  • %comment% – The comment (note) made when sharing an item

The formatting for start and end is:

  • %googlereader% – Link to your Google Reader shared page
  • %count% – Number of items shown

Download googlereader.zip

162 Comments

  1. I just installed this widget, and it works like a charm. But is it possible to get it to show the name of the blog for each item?

  2. Hi,

    I’m new to WordPress and have no coding experience, but I’ve done a good job in following instructions to fix problems with building my blog so far. The Google Reader widget you’ve designed works wonderfully on my blog, but I’m having a problem with “& g t ;” appearing where a “>” should appear on news feeds I’ve posted? (As in “NY Times > Home Page” reads NY Times & g t ; Home Page) You can see it at:

    grottomazoo.com

    Any help you can offer would be appreciated.

  3. Mazoo, your blog’s DB is down, but I found and fixed the problem.

    For the RSS feed, all &’s are changes to &amp;, so I changed it to replace all instances to just &, but it’ll go back through to make sure the & wasn’t just an &amp;

    I added this on lines 58-59:

    $stories = str_replace("&", "&", $stories);
    $stories = preg_replace("/&(?!(amp|[#0-9] |lt|gt|quot|copy|nbsp);)/ix","&",$stories);
    

    Just download the files again, and replace googlereader.php with the new version.

    Let me know if you encounter any other problems.

  4. Steve, you didn’t really create anything. You’re just using what is automatically generated by Google Reader and copying it into a text widget. That relies on Javascript, which means a longer loading time and won’t always work. Plus, only so much can be done with it.

    And you don’t need any programming knowledge to use this. Knowing HTML can help if you want to customize it though.

  5. This widget looks really interesting, but I can’t get it to work. I uploaded it, activated it and I can drag and drop and configure it in the widgets-management. It shows up correctly, and the link to my shared page that is placed at the end is correct – but no items show up at all in the sidebar (except for the “title”, “items start” and “items end”-code.

    I have shared four items, which are displayed on my shared items page in Google and when I check the feed from Google, it contains data. Anyone else had this problem?

  6. Ahem. Nevermind. My hosting provider turned off file_get_contents(); for security reasons. I changed the code to use curl(); and voila!, it works.

    Thanks for a great widget!

  7. It’s just the interval that it will check your shared items RSS feed.

    If you set it to 0 or -1, it’ll check every page load, which will slow down the page loading time since it has to request and parse the content, instead of just echoing the cached content.

    3600 means it won’t check updates if it has checked less 1 hour (3600 seconds) ago. It should be good time for most people.

  8. Makes sense. Thanks for the response. And congrats a very nice, easy to use, plug-in. I tried a couple other Google Reader feed widgets. None were as nicely laid out or easy to install/administrate as yours.

  9. Mike, thought I responded earlier to your comment, but yes, there should be a multiple widget version soon. I’ve been working on one for the YouTube widget. Once I get that working and finding the best/quickest way to implement it, I’ll probably start adding it to each widget.

  10. Google Reader for your cell phone.

    Asterbox.com announces the beta release of their Java mobile platform. They have openly declared what many users have felt: surfing the web on a mobile device is a disappointing experience. Their claims that the internet on the phone is dead, but can live on through web services have been embodied in the Asterbox mobile platform. The Java-based platform utilizes web APIs to deliver content and interactivity to end users in a new and innovative way. The software currently provides access to the Google Reader service, but more functionality is in the works.

  11. i’m trying to use the Google Reader Widget, and getting this error: Warning: file_get_contents(http://www.google.com/reader/public/atom/user/10503069598462794774 /state/com.google/broadcast?n=9): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in /html/wp/wp-content/plugins/googlereader.php on line 55

    line 55 is

    $stories = file_get_contents($uri);

    so i thought that maybe this was the same problem as micke’s hosting problem. But when I replace file_get_contents with curl();, it gives me another error:

    Fatal error: Call to undefined function: curl() in /html/wp/wp-content/plugins/googlereader.php on line 55

    Any ideas?

  12. micke, can you tell me a little more about using curl();? do you have to load libraries or anything? i tried changing the “file_get_contents()” to “curl()” and its not working…

  13. jpb, from the looks of it, it seems you have a space after your user id which makes the url actually http://www.google.com/reader/public/atom/user/10503069598462794774%%20/state/com.google/broadcast?n=9. (only 1 %, since WordPress changes %20 automatically to a space) which doesn’t exist, but http://www.google.com/reader/public/atom/user/10503069598462794774/state/com.google/broadcast?n=9 does work.

    Make sure your user id doesn’t have an extra spaces or new lines or anything at the end (or beginning). It’s saying the actual url is returning a 404, so it not really the function having problems, and using curl, you should still run into the same problem.

    To make sure you don’t have that problem, you can change:

    $userid = urlencode($options['userid']);

    to

    $userid = preg_replace("/[^0-9] /ix", "", $options['userid']);

    It will remove all non-numeric characters.

    And to use curl, you don’t just use a curl() function, or at least I don’t think you do.

    Pretty sure you would have to do something like:

    $curl_handle=curl_init();
    curl_setopt($curl_handle,CURLOPT_URL,$uri);
    curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
    curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
    $stories = curl_exec($curl_handle);
    curl_close($curl_handle);
    
  14. ah_skeet, yr a saint. Good catch on the trailing space, and thanks for taking the time to lay this all out for me. Be assured that I’ll come to you every time I have a technical problem. 😉

    Thanks again!

  15. Haha, no problem.

    Also, I thought I had fixed this, but somewhere down the line, I broke it again.

    In Widget Formatting, change %site to %site% to display the site name instead of %site.

  16. Thanks again! I’ve been poking around, trying to figure that “%site% problem just now. The trailing “%” did the trick.

  17. FYI, those who are on a host that doesn’t allow file_get_contents…

    here’s the exact code you need to slap in there:

    // replacing with cURL
    $ch = curl_init();
    $timeout = 5; // set to zero for no timeout
    curl_setopt ($ch, CURLOPT_URL, $uri);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $stories = curl_exec($ch);
    curl_close($ch);

    //$stories = file_get_contents($uri);

  18. I think on line 47
    echo $before_widget.$widgettitle.$options['cache'].$afterwidget;
    should be
    echo $before_widget.$widgettitle.$options['cache'].$after_widget;

    Bye!

  19. Thanks for this. Tried two others that didn’t have the code that you did for parsing out curly quotes and other sundry HTML nonsense. Very nicely coded and quite handy for simple stuff, or even more complex. Finally someone beat Google at their own game. Thanks a million. I owe you!

  20. you rock. seriously. i’ve been messing with rss widgets and feedburner and other crap, all to no avail. this widget worked like a freaking charm. thank you!

  21. I was wondering if this plugin has been tested with WP 2.3.1? Or if their are hacks to make it run?

    I love the idea, and would love to see it progressing. I would be of more help, but my programming skills are novice at best.

    Thanks for your time.

  22. Nice! Works like a charm. I was having a lot of problems with the RSS widget. It was taking the URL from the item link and summary xml:base and concatenating them together.

    Anyway, Thanks!

  23. As far as I can tell it doesn’t work with 2.3.1…if there is a way to get this to work I would be very interested as I love the idea.

  24. Jonathan: doesn’t it work for you? I upgraded my installation last week, and I’m running the Google Reader Widget without any problems. Did it work for you before upgrading to 2.3.1?

  25. Thanks for making this! I’d like to use other Google Reader public feeds (i.e. not always just the Shared Items). Are you planning changes like this, or should I take a bang at it using your code? How are the multiple widget changes coming?

  26. @ Charl,

    I wasn’t really planning on doing really any changes, unless Google did something to break it.

    The multiple widgets thing were coming, but the way I was doing it got broken in one of WordPress’s updates. I’ll try working on this break.