Adding Custom RSS Feed to WordPress Dashboard

The internet is a vast resource, and it can be overwhelming to keep track of everything. While Twitter and RSS readers are great tools, having multiple software open at once can be frustrating. That’s why using the WordPress dashboard as a global platform is a great solution. In this article, we’ll show you how to create a custom RSS metabox within the WordPress dashboard.

Step 1: The Plugin

To add this metabox, we need to create a plugin. 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. The code below is the code that will generate the plugin:

/*

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, we need to register at least one metabox to be displayed on the WordPress dashboard. To do so, we have to create a new function that will hook the “wp_dashboard_setup” hook. Let’s call this function “rc_mdm_register_widgets()”. Inside this function, we need to tell WordPress that we want to add a new metabox, and this is the aim of the “wp_add_dashboard_widget()” function. This function accepts four 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.

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

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

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 built-in “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 to 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-known WordPress functions or are included in 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 = ‘‘;

echo ‘‘.$rss_title.’‘;

echo ‘


‘;

// Starts items listing within

‘;

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

We’ve created a simple metabox, but you now have the basics to create your 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.

Stay in Touch

spot_img

Related Articles