There are tons of themes and plugins coming out for WordPress every day, but none of them seems to utilize this feature. WordPress developers should add a custom dashboard widget with support information. In this post, I will show you how you can add RSS feed widgets in the WordPress dashboard.
The anatomy of a dashboard widget is extremely simple. First, you need to use the wp_add_dashboard_widget()
function to register it with WordPress. Then you create a function that fetches and display RSS feed from your blog.
Here's the template code you can use to create one quickly. Please don't forget that this should be placed in a plugin or your theme's functions.php
file.
The first step is to hook a function into wp_dashboard_setup
. The content of this function is a simple call to wp_add_dashboard_widget()
with three parameters:
- Widget slug (lj_dashboard_widget)
- Widget title (Latest Posts From Linesh.Com)
- Display/callback function (lj_rss_dashboard_widget)
// Hoook into the 'wp_dashboard_setup' action to register our other functions add_action('wp_dashboard_setup', 'lj_add_dashboard_widgets' ); // Create the function use in the action hook function lj_add_dashboard_widgets() { wp_add_dashboard_widget('lj_dashboard_widget', 'Latest Posts From Linesh.Com', 'lj_rss_dashboard_widget'); }
The second step is creating the display function lj_rss_dashboard_widget()
and filling it with content.
function lj_rss_dashboard_widget() { $rss = @fetch_feed( 'https://linesh.com/feed/' ); if ( is_wp_error($rss) && ( is_admin() || current_user_can('manage_options') ) ) { echo '<div class="rss-widget"><p>'; printf(__('<strong>RSS Error</strong>: %s'), $rss->get_error_message()); echo '</p></div>'; } elseif ( !$rss->get_item_quantity() ) { $rss->__destruct(); unset($rss); return false; } else { echo '<ul class="rss-widget">'; $maxitems = $rss->get_item_quantity(10); $rss_items = $rss->get_items(0, $maxitems); foreach ($rss_items as $item){ echo ' <li> <span class="rss-date">'.$item->get_date('j M Y').'</span>: <a class="rsswidget" href="'.$item->get_permalink().'" />'.$item->get_title().'</a> </li>'; } //wp_widget_rss_output( $rss ); echo '</ul>'; $rss->__destruct(); unset($rss); } }
In this function, you can fetch RSS feeds from Linesh.com(or any other site’s feed) usingfetch_feed()
the function. You can print feed items with WordPress builtin function wp_widget_rss_output()
or you can fetch all items as an array with$rss->get_items()
function and display as you like.
Result:
Remember to change the feed URL and add other useful information.