wordpress-rss-feed-widget

Build your own WordPress dashboard widget for any RSS feed

It’s easy to add your own dashboard widget to WordPress, and one of the easiest to widgets to add is one that displays recent updates from any RSS feed. I put together the above widget for a project I am working on so we can promote Lawyerist posts to users.

Here’s what goes into the theme’s functions.php (or custom_functions.php, in the case of Thesis) file:

function dashboard_widget_function() {
     $rss = fetch_feed( "http://lawyerist.com/feed/" );
 
     if ( is_wp_error($rss) ) {
          if ( is_admin() || current_user_can('manage_options') ) {
               echo '<p>';
               printf(__('<strong>RSS Error</strong>: %s'), $rss->get_error_message());
               echo '</p>';
          }
     return;
}
 
if ( !$rss->get_item_quantity() ) {
     echo '<p>Apparently, there are no updates to show!</p>';
     $rss->__destruct();
     unset($rss);
     return;
}
 
echo "<ul>\n";
 
if ( !isset($items) )
     $items = 5;
 
     foreach ( $rss->get_items(0, $items) as $item ) {
          $publisher = '';
          $site_link = '';
          $link = '';
          $content = '';
          $date = '';
          $link = esc_url( strip_tags( $item->get_link() ) );
		  $title = esc_html( $item->get_title() );
          $content = $item->get_content();
          $content = wp_html_excerpt($content, 250) . ' ...';
 
         echo "<li><a class='rsswidget' href='$link'>$title</a>\n<div class='rssSummary'>$content</div>\n";
}
 
echo "</ul>\n";
$rss->__destruct();
unset($rss);
}

function add_dashboard_widget() {
     wp_add_dashboard_widget('lawyerist_dashboard_widget', 'Recent Posts from Lawyerist.com', 'dashboard_widget_function');
}

add_action('wp_dashboard_setup', 'add_dashboard_widget');

Note that there are two different functions:

  1. dashboard_widget_function defines the actual widget
  2. add_dashboard_widget adds your widget to the dashboard and gives it an id (“lawyerist_dashboard_widget”, in this case) and title (“Recent Posts from Lawyerist.com”)

To change the feed, change the URI in this line:

$rss = fetch_feed( "http://lawyerist.com/feed/" );

To change the id for the widget and the title that displays to users, change this line:

wp_add_dashboard_widget('lawyerist_dashboard_widget', 'Recent Posts from Lawyerist.com', 'lawyerist_dashboard_widget_function');

And that’s it. You’re done!

8 thoughts on “Build your own WordPress dashboard widget for any RSS feed”

  1. Hi,

    I just used your code to create a dashboard widget that appears on all the blogs in a Multisite Network, with the feed coming from a category on the main blog.

    While setting it up, I created a “twst post.” The spelling was unintentional, so I went back and edited the post by changing the title, the slug, and the content.

    If I click on the title, it takes me to the correct post, but even after refreshing all the dashboards where the feed appears, or reloading the browser, the feed title still shows “Twst Post” and I cannot figure out how to update the display title.

    Any advice?

    1. Since the widget is powered by an RSS feed, check the feed to see if the title is still showing up wrong on the feed.

      My guess is that your feed hasn’t yet picked up the updated title. It should, eventually, but I’m not sure how quickly feeds get updated.

      1. It is showing up properly in the feed at http://taxtutor.com/category/blog/feed/ but the dashboard widget is still showing “twst blog” as the title of the first item.

        Does the feed store items somewhere? Clicking on that link sends readers to the new post, since the old one was relegated to the trash. I had changed the title to this title, but the slug is different. The new one has a dash two added as a suffix since there was already a post of the same name.

        The behavior seems a bit weird to me.

          1. I didn’t include it as a Feedburner feed as I am using it to send notices to admins in my multisite network. The feed you are seeing is for a blog used as a template when sites are replicated.

            I also went to my database using PHP My Admin and searched for the exact phrase twst post. Nothing.

            It’s a mystery Sam, it am.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>