I recently wanted to learn more about how the WordPress Dashboard Widget API works so I created an example plugin to test it out. The plugin is a dashboard widget version of my WordPress AJAX Example plugin’s admin page. It creates a table where you can type in an option ID and then an AJAX call will provide you with information regarding that option. The README for the plugin can be found below (embedded using my DobsonDev Shortcodes plugin which now also has a GUI to make adding the shortcodes easier).
You can download the Dashboard Widget Example plugin from my GitHub.
I’ve gone over making plugins before on my blog and I also wrote up an article describing my WordPress AJAX Tester plugin – so for this article I won’t go over those parts of this plugin. The only section of code we really need to go over is the call to wp_add_dashboard_widget( … )
. You can see that section below:
This very small bit of code is all that’s needed to create a Dashboard Widget. You give it a slug, title and a display function (this function will contain the actual HTML to display/output). It’s worthy to note that you can also give the widget a WID which will allow you to create, retrieve and update options specific for your widget. I don’t really go over how to do that here but if you want to know more see the Codex pages on the Dashboard Widgets API and creating an Example Dashboard Widget.
The function that displays inside the widget is nothing to write home about, but I’ve included it anyway just because it’s tied to the above function:
And that’s pretty much all there is to it. Try out the plugin yourself and read over the code – modify it to include some options. Creating a WordPress Dashboard Widget is actually extremely easy so if you play around with it you can create something pretty awesome in no time flat. As always thank you for reading and please share it around as much as you can! Please feel free to put any suggestions or ideas for future tutorials in the comments section below.