This is a guest post by Konstantin Kovshenin — a WordPress core contributor, developer and consultant. Editor in chief of Theme.fm — tutorials, tips and tricks on WordPress themes and plugins, setup, security and performance, as well as WordPress consulting. Follow Konstantin on Twitter and don’t hesitate to poke him should you need any help.

WordPress themes (both free and paid) nowadays come bundled with a lot of different kind of options — color schemes, custom post types, portfolio pages, contact forms, custom shortcodes and much more. These are the kind of things developers usually offer to their users, but what do they get in return? Well if it’s a paid theme they obviously get some money and if it’s a free one, donations are more common. But there’s one thing you always get in return as a WordPress theme (or plugin) author, and that’s awareness.

You may also be interested in these articles:

Whether it’s yours, or your brand’s, awareness is what gets people to know you in the community, therefore building trust. Trust brings you more visitors to your website or blog, more chances of getting hired for a project and of course a little bit of fame and respect.

Today’s topic is a Dashboard Feed Reader, which is quite a simple widget hanging in your admin dashboard screen, showing the latest stories from your blog. Such widgets often drive more traffic to your website and gets people to read (at least) your headlines directly from their WordPress admin panel. A dashboard reader is a great way to announce new versions of your themes or plugins, as well as promote your new products.

People often think that dashboard readers are distracting or annoying, but the good thing about WordPress is that the Dashboard is actually configurable. In only a couple of clicks, any user can enable or disable any of their dashboard widgets, leaving on screen only the stuff that’s important to them. It’s done via the Screen Options button at the top right of the Dashboard page. So dashboard readers are way more loyal and user friendly than hard-coded links or banners on the front end of your theme or plugin.

Understanding Dashboard Widgets

Dashboard widgets in WordPress are actually meta boxes. They’re used in other places too, like the Edit Post or Page screen. They can be dragged around, shown or hidden from the Screen Options menu, collapsed and expanded by hitting the tiny arrow on the right side of the box titles. But hey, you should probably know that already!

Meta boxes are registered with the add_meta_box function but the ones on the Dashboard screen have a function of their own called wp_add_dashboard_widget which should be used during the wp_dashboard_setup WordPress action. So with that in mind, let’s go ahead and write some code.

Creating a New Dashboard Widget

In this tutorial I’ll be creating a dashboard feed reader for Theme.fm. The reader can then be bundled in a theme or plugin and the implementation doesn’t differ at all. So in your plugin file or the functions.php file of your theme, type in the following snippet.

It’s quite simple to understand what it does. I’ve got two functions, one for the dashboard setup and another one for the actual content. The content function is used as a callback during the setup of the widget, while the setup function is used in an action as I mentioned earlier. You can furthermore nest this into an admin_init action, since the dashboard is not available elsewhere, but that won’t have any noticeable impact on your site’s performance.

If you browse to your Dashboard at this stage, you’ll see a new widget called Latest from Theme.fm with the contents that we wrote in the second function. Guess what? You’re done! With the widget, that is… That’s all there is to it adding new dashboard widgets, you can move it around, expand or collapse it, and even turn it off and back on from the Screen Options panel.

Now, before you start shipping this piece of code with all your new themes and plugins, let’s fill it in with some actual content. You can go ahead with “lorem ipsum” but I suggest getting the content from your RSS feed, so that’s what we’ll do next.

Gathering Content From Your RSS Feed

WordPress itself comes bundled with SimplePie and FeedCache to work with the internal stuff, which means that we can use the exact same functions for our own functions. SimplePie is quite easy to work with as you’ll see in a second. WordPress allows a neat function called fetch_feed which could be used out of the box, provides feed parsing and caching and all that for free, without any extra coding. Go ahead and try it out in that content area where you were about to stick “lorem ipsum” ;)

This will print all the feed contents (if there was no error retrieving them) in a nicely structured object. Get familiar with the structure, see what content is available and what’s not. There are neat SimplePie functions that we can use against that object to retrieve the data in the format we want, like dates for instance.

To spare you the hassle, here’s what my content function looks like after implementing the feed contents:

So double check the $feed variable in case it returned an error. Set the maximum number of items to yield to 5 and then, with a simple foreach loop, walk through all the items and print them out in a nice looking way — with permalinks, dates and descriptions.

That’s about it, that’s how easy it is to use SimplePie inside WordPress and again, caching is given to you for free, without even having to activate it or whatsoever, so if you refresh the dashboard screen you won’t notice any lags at all! Here’s what your new dashboard screen should look like, now with the new feed reader:

Congratulations, you’ve just built yourself a feed reader, which you can bundle in your themes and plugins, free and paid ones. You just got your users to read your blog directly from their admin panels, unless the deactivate you of course ;)

Conclusion

So in this tutorial you’ve seen how to create your very own dashboard feed reader, getting more readers to your blog and awareness to you or your brand. In only a few lines of code, we’ve registered a new dashboard widget, then used SimplePie to fetch an RSS feed and display the content, with built-in caching.

A word of caution is that if you’re planning to bundle the reader in numerous themes and plugins, think about what will happen if somebody activates one of your theme, together with three of your plugins? You don’t want your users to end up with four dashboard readers, right? There are numerous techniques to avoid this, my favorite one is constants. I basically wrapped up the whole widget code into a class which I initialize during admin_init. I then wrapped the code into a simple if statement, like this:

Including the same constant in every theme and plugin you ship, you’ll make sure that only one instance of the feed reader is created. You might also want to use the class_exists PHP function if you’re planning to use the same class names for your reader in all the themes and plugins.

Well, that’s about it! Hope you enjoyed this tutorial and make sure you use it with caution, plus test before shipping. Thank you so much for reading and hope to see you again soon!

About The Author

Konstantin Kovshenin is a WordPress core contributor, developer and consultant. Editor in chief of Theme.fm -- tutorials, tips and tricks on WordPress themes and plugins, setup, security and performance, as well as WordPress consulting. Follow Konstantin on Twitter and don't hesitate to poke him should you need any help.

Hi Konstantin, quick question about your widget: did you move it yourself to the top or do you control that from within the code?
If you manually moved it to show on the left top, could you also show a way to make sure that happens from within the code?
Cheers,
Piet