Nerdanita’s corner

How to add a custom news feed to the WordPress Dashboard

on March 6, 2012

I was working in a project for a local Radio that also publishes news, and is based in WordPress. And I thought it would be great if they could have a news feed integrated in the WordPress Dashboard that allows them to consume news from every News Agency they wanted. By this, they wouldn’t have to leave the tool they use to look for news outside.

So, I searched in the Internet if someone did something like that before, and I found this:


add_action('wp_dashboard_setup', 'my_dashboard_widgets');

function my_dashboard_widgets() {
global $wp_meta_boxes;
unset(
$wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins'],
$wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary'],
$wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']
);
wp_add_dashboard_widget( 'dashboard_custom_feed', 'YOUR_FEED_NAME' , 'dashboard_custom_feed_output' );
}


function dashboard_custom_feed_output() {
echo '<div>';
wp_widget_rss_output(array(
'url' => 'http://wpsnipp.com/feed/',
'title' => 'MY_FEED_TITLE',
'items' => 2,
'show_summary' => 1,
'show_author' => 0,
'show_date' => 1
));
echo '</div>';
}

Let see what this code means:

add_action('wp_dashboard_setup', 'my_dashboard_widgets');

To run the function you will need to hook into the correct action, in this case, ‘wp_dashboard_setup’. So this basically says “add my_dashboard_widgets” to all the functions that WP runs when the Dashboard is being set.

This part of the code:
unset(
$wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins'],
$wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary'],
$wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']
);

This lines just delete the widgets that shows “Plugins”, “Other WordPress News” rss and “WordPress Blog” rss. So they are optionals, if you want to keep this widgets, just delete this lines from your function.

This line:

   wp_add_dashboard_widget( 'dashboard_custom_feed', 'YOUR_FEED_NAME' , 'dashboard_custom_feed_output'

Is the main function, it allows you to add a new widget to the dashboard, and you can check the full description here

Finally

    wp_widget_rss_output(array(
'url' => 'http://wpsnipp.com/feed/',
'title' => 'MY_FEED_TITLE',
'items' => 2,
'show_summary' => 1,
'show_author' => 0,
'show_date' => 1
));

Here is where we set the custom values for our news feed.

All this have to be pasted in functions.php under your theme folder, and voilá! You have your news feed widget in the Dashboard.

Hope this is useful, and see you next time!

PS: I have to mention this post, that is the source of my post😉


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: