Make Your Own Latest News Dashboard Widget

Posted on

After upgrading one of our multisite installs to WP 3.1.2 we noticed a problem: no longer is there a dashboard widget that shows the most recent post from the main site. To make matters worse, the option where you could adjust this with, also seems to have vanished. Over at wpmudev they noticed this too, but nobody has come up with a solution.

We decided not to wait for an answer, but figure something useful out for ourselves. However, probably one of the least documented areas of WordPress is how to customize the Dashboard. Sure you can find how to remove some of the standard widgets that are shown there, whether you do that through the screen options panel or by removing (some of) them through your functions.php file.

An example of the latter removing some of them through your functions.php file:

// disable default dashboard widgets
function disable_default_dashboard_widgets() {
	remove_meta_box('dashboard_quick_press', 'dashboard', 'core');
	remove_meta_box('dashboard_primary', 'dashboard', 'core');
	remove_meta_box('dashboard_secondary', 'dashboard', 'core');
}
add_action('admin_menu', 'disable_default_dashboard_widgets');

It’s also not that difficult to find some code snippet to add a sentence or two, perhaps even with a link, to the dashboard, but it quickly becomes more complicated when you really would like to add something useful to the Dashboard.

On the sites hanging under the main site of the multisite install, we wanted to show this new Dashboard Widget. It should show above all other widgets and it should have a different background color than the other widgets in the Dashboard.
Furthermore the content of our new Dashboard widget should be the latest 3 entries of the RSS-feed of the main site, obviously with links going to those Posts and the hover color of those links should also differ from the standard.

First we looked through a few plugins that we know add their own RSS-feeds to the Dashboard, to see how they do it. One of those plugins (WPML) has a separate file for it, appropriately called dashboard-widget.php. And in that file the function fetch_feed() is called.
But the way WPML integrates this file, which contains quite a bit of other code too, was a bit too complicated. So we needed to look further. If you Google for Dashboard widgets you can find lots of stuff, but nothing helped us further, so our last resort – which should have been our first – was WordPress Answers.

After a bit looking around we found a function snippet that would become the frame of our own function to add the Latest News Dashboard Widget.

Now all we needed to do was to combine the two snippets into our own function. Here is what we came up with:

Add to your functions.php file

// Add SHC Feed Dashboard Widget
function shcfeed_setup_function() {
    add_meta_box( 'shcfeed_widget', 'Latest News Senlin Hosting Club', 'shcfeed_widget_function', 'dashboard', 'normal', 'high' );
}
function shcfeed_widget_function() {
    include ('shcfeed.php'); // new file where RSS Feed will be called from
	echo '<style type="text/css">
	#shcfeed_widget { background: #FFFF99;}
	#shcfeed_widget a:hover, #shcfeed_widget a:active, #shcfeed_widget a:focus {color: #003366;}
	#shcfeed_widget .handlediv {display:none;}
	</style>';
}

add_action( 'wp_dashboard_setup', 'shcfeed_setup_function' );

Obviously you can call this function anything you like, remember that if you are going to change it, you have to change it everywhere. On line 3 it is important that you change the wording as that becomes the header text of the box. If you don’t need or want your widget to show up with the highest priority, then you can change ‘high’ (back) into ‘low’.

On line 6 you see the name of the file we include. You have to make this file separately and SFTP that to your server.
In lines 7-11 we have echoed the CSS styles we would like this new widget to use.

The new file where your RSS feed will be called from

<?php
$rss = fetch_feed('domain.com/feed/'); //change RSS feed to your own
if (!is_wp_error($rss)) { // Checks that the object is created correctly
    // Figure out how many total items there are, but limit it to 3.
    $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);
}
if (!empty($maxitems)) {
?>
	<div class="rss-widget">
    	<ul>
<?php
    // Loop through each feed item and display each item as a hyperlink.
    foreach ($rss_items as $item) {

?>
			<li><a class="rsswidget" href='<?php echo $item->get_permalink(); ?>'><?php echo $item->get_title(); ?></a> <span class="rss-date"><?php echo $item->get_date('j F Y'); ?></span></li>
<?php } ?>
		</ul>
	</div>
<?php
}
?>

Change the URL of your feed on line 2 and save this file with the same name as you have used to call it from in your functions.php file – in our case “shcfeed.php” – and SFTP it together with your saved functions.php file to the theme directory on your server.

End Result

We have uploaded an image showing the new Dashboard Widget in action:
Latest News Dashboard Widget

How do you use Dashboard Widgets on the websites of your clients?

7 comments on “Make Your Own Latest News Dashboard Widget

  1. Pingback: Latest News Dashboard Widget

  2. A friend of mine could really use this plug-in! I will forward this link over to him. I don’t know if it works for me just yet as I don’t have multi-sites….good to know! AND thanks for sharing this information. Very cool. – Jan

    • Hi Jan, thanks for your comment.
      You can actually use this for any site, not jut for multisite installs.

  3. Pingback: Multisite Dashboard Feed Widget plugin released

  4. Pingback: WordPress Multisite Dashboard Feed Widget Show Recent Posts | Wordpress Multisite Blogs Help Tips | Behind the Scenes

    • perhaps an idea to actually read an article before leaving a comment? Your question is answered right after the 2nd paragraph. Not sure what more you want…

Comments are closed.