Skip to main content
WordPress made easy with the drag & drop Total WordPress Theme!Learn More

How to Add a Custom RSS Feed in the WordPress Dashboard

Last updated on:
Add a Custom RSS Feed in the WordPress Dashboard

The Internet is full of great resource and it’s hard to have a look on everything. Of course there’s Twitter to keep an eye on tendencies, or RSS reader softwares, but you are like me very busy it’s sometimes annoying to have 20 softwares opened at the same time. That’s why i decided to use my WordPress dashboard as a global platform to have a quick access to those feeds. An example i want to share today is how to create a custom RSS metabox within the WordPress dashboard.

Here is the final result of what we’re going to create:

add-a-custom-rss-dashboard-metabox

Step 1: The Plugin

To add this metabox we need to create a plugin. So, simply create a new folder called “my-dashboard-metaboxes” in wp-content/plugins/ and within the new folder create a file called my-dashboard-metaboxes.php. This file will be the main plugin file. Open it into your main editor. The code below is the code that will generate the plugin. Nothing really complex here:

<?php
/*
Plugin Name: My Dashboard Metaboxes
Plugin URL: http://remicorson.com/
Description: Adds custom meta boxes on the main admin dashboard
Version: 0.1
Author: Remi Corson
Author URI: http://remicorson.com
Contributors: corsonr
Text Domain: rc_mdm
*/

Step 2: Registering the Metabox

Now that we have an empty plugin (i mean a plugin that doesn’t do anything), we need to register at least a metabox to be displayed on the WordPress dashboard. To do so, we have to create a new function that will hooked the “wp_dashboard_setup” hook. Let’s call this function “rc_mdm_register_widgets()”. Inside this function we need to telle WordPress that we want to add a new metabox, and this is the aim of the “wp_add_dashboard_widget()” function. This function accepts 4 parameters:

1 – $widget_id (integer) (required) an identifying slug for your widget. This will be used as its css class and its key in the array of widgets.
Default: None

2 – $widget_name (string) (required) this is the name your widget will display in its heading.
Default: None

3 – $callback (string) (required) The name of a function you create that will display the actual contents of your widget.
Default: None

4 – $control_callback (string) (optional) The name of a function you create that will handle submission of widget options (configuration) forms, and will also display the form elements.

What’s important here is the third parameter, it’s the one that defines the functions that will be loaded into the metabox. In this example it’s called “rc_mdm_create_my_rss_box()”.

/**
 * Register all dashboard metaboxes
 *
 * @access      public
 * @since       1.0 
 * @return      void
*/

function rc_mdm_register_widgets() {
	global $wp_meta_boxes;
	
	wp_add_dashboard_widget('widget_freelanceswitch', __('My RSS Feeds', 'rc_mdm'), 'rc_mdm_create_my_rss_box');
}
add_action('wp_dashboard_setup', 'rc_mdm_register_widgets');

Step 3: The Metabox Content

If you activate the plugin and go to your WordPress dashboard you should see a new empty metabox. We now need to fill in its content. Important things for this function is to include the WordPress builtin “feed.php” file to be allowed to use the “fetch_feed()” function. Please note we are using “fetch_feed()” because “fetch_rss()”, “get_rss()”, and “wp_rss()” are deprecated. For once, i included all comments within the code directly but i would like to draw your attention on some nice features i’m using inside the metabox function.

First of all there’s the “fetch_feed()” function. This one is used to get and parse the feeds content. This function is using the SimplePie class, so you can take advantage of nearly all functions included in it.

We then have the “human_time_diff()” function that is used to display the time as “human_time_diff()“, for example to display something like “2 hours ago”, “4 days ago” etc… it’s a WordPress function.

And finally we have “wp_html_excerpt()” to shorten each feed content.

All other functions are well know WordPress functions or are included into the Simple Pie class.

Here is the code:

/**
 * Creates the RSS metabox
 *
 * @access      public
 * @since       1.0 
 * @return      void
*/

function rc_mdm_create_my_rss_box() {
	
	// Get RSS Feed(s)
	include_once(ABSPATH . WPINC . '/feed.php');
	
	// My feeds list (add your own RSS feeds urls)
	$my_feeds = array( 
				'http://feeds.feedburner.com/FSAllJobs', 
				'http://www.wphired.com/feed/?post_type=job_listing' 
				);
	
	// Loop through Feeds
	foreach ( $my_feeds as $feed) :
	
		// Get a SimplePie feed object from the specified feed source.
		$rss = fetch_feed( $feed );
		if (!is_wp_error( $rss ) ) : // Checks that the object is created correctly 
		    // Figure out how many total items there are, and choose a limit 
		    $maxitems = $rss->get_item_quantity( 3 ); 
		
		    // Build an array of all the items, starting with element 0 (first element).
		    $rss_items = $rss->get_items( 0, $maxitems ); 
	
		    // Get RSS title
		    $rss_title = '<a href="'.$rss->get_permalink().'" target="_blank">'.strtoupper( $rss->get_title() ).'</a>'; 
		endif;
	
		// Display the container
		echo '<div class="rss-widget">';
		echo '<strong>'.$rss_title.'</strong>';
		echo '<hr style="border: 0; background-color: #DFDFDF; height: 1px;">';
		
		// Starts items listing within <ul> tag
		echo '<ul>';
		
		// Check items
		if ( $maxitems == 0 ) {
			echo '<li>'.__( 'No item', 'rc_mdm').'.</li>';
		} else {
			// Loop through each feed item and display each item as a hyperlink.
			foreach ( $rss_items as $item ) :
				// Uncomment line below to display non human date
				//$item_date = $item->get_date( get_option('date_format').' @ '.get_option('time_format') );
				
				// Get human date (comment if you want to use non human date)
				$item_date = human_time_diff( $item->get_date('U'), current_time('timestamp')).' '.__( 'ago', 'rc_mdm' );
				
				// Start displaying item content within a <li> tag
				echo '<li>';
				// create item link
				echo '<a href="'.esc_url( $item->get_permalink() ).'" title="'.$item_date.'">';
				// Get item title
				echo esc_html( $item->get_title() );
				echo '</a>';
				// Display date
				echo ' <span class="rss-date">'.$item_date.'</span><br />';
				// Get item content
				$content = $item->get_content();
				// Shorten content
				$content = wp_html_excerpt($content, 120) . ' [...]';
				// Display content
				echo $content;
				// End <li> tag
				echo '</li>';
			endforeach;
		}
		// End <ul> tag
		echo '</ul></div>';

	endforeach; // End foreach feed
}

At line 15 there’s an array where you can put as many feeds as you want. You can also define the number of each feed items to display on line 27. Finally on line 50 and 54 you can choose to display a human date or a normal date. It’s up to you.

Conclusion

Ok, so we created a simple metabox, but you now have the basics to create you own metaboxes with your own content. You can also remove default WordPress metaboxes, and to have a full comprehension of the dashboard widgets API, i encourage you, as always, to have a look at the codex.

rss-dahboard-metabox-wordpress
Article by Remi WPExplorer.com guest author
Subscribe to the Newsletter

Get our latest news, tutorials, guides, tips & deals delivered to your inbox.

5 Comments

  1. Ceren Akın

    I tried it and it’s great but I have a minor problem that I couldn’t find a solution for:
    It pulls the rss feed alright, but when the rss feed updates the feed on my dashboard is not updated. If I upload the plugin on another wordpress site it shows the new feeds, but when the feed updates again it doesn’t load the new feeds on that site too. I checked the code and it doesn’t even load the new feeds as a element. I also tried to deactivate and activate the plugin but still didn’t work. Is it a problem about my website or is it a known issue?

  2. Sergio

    Thanks for sharing it!, but I wonder if there’s something like do_shortcode(‘rss_widget’) already doing that?

    • AJ Clarke

      This is a dashboard metabox so you can’t display it like a shortcode. You can pretty much copy/paste the code though into a widget function to display a feed. It’s really easy to create a new widget see the CODEX.

  3. Florian Schroiff

    This can now be done with way less code:

    /**
     * Register the dashboard widget.
     *
     */
    add_action( 'wp_dashboard_setup', 'my_dashboard_setup' );
    function my_dashboard_setup() {
    	add_meta_box( 'my_output_dashboard_widget', esc_html__( 'Recent posts from My website', 'my-textdomain' ), 'my_output_dashboard_widget', 'dashboard', 'side', 'high' );
    }
    
    /**
     * Echo the dashboard widget.
     *
     * @param int $items
     *
     */
    function my_output_dashboard_widget( $items = 10 ) {
    	echo '';
    	wp_widget_rss_output( 'http://mysite.com/feed/'), array( 'items' =&gt; $items ) );
    	echo '';
    }
    
    • AJ Clarke

      Awesome, great for sharing! I’ll update the post 😉

Leave a Reply

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

Learn how your comment data is processed by viewing our privacy policy here.