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).
DobsonDev WordPress Dashboard Widget Example
A simple plugin for illustrating how to use the WordPress Dashboard Widget API. The example provided is extremely simple - but for more information see the Codex pages on the Dashboard Widgets API and creating an Example Dashboard Widget. All of the code for this plugin was adapted from those pages as well as my own DobsonDev WordPress AJAX Tester plugin.
The dashboard widget created by this plugin provides an AJAX interface where you can call on your WordPress options table and display information about them based on their ID. The option ID, name, value and if it autoloads or not are all displayed in the table. As mentioned briefly above, the widget uses AJAX calls so you can check out as many options as you want without ever having to reload or leave the page.
The real point of this plugin is to look through the code and see how the WordPress dashboard widget API works with a real example. This plugin also demonstrates how AJAX in WordPress plugins works (which can also be seen in my DobsonDev WordPress AJAX Tester plugin).
To install simply add this folder to your plugins folder (most commonly located at '/wp-content/plugins/').
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.