WordPress comes with a bunch of default dashboard widgets. Me? I appreciate them: I’m a WordPress super-nerd, so when I see “Other WordPress News” and such things, the compulsive clicking starts.
When we make a site for a client, however, they don’t need to see that sort of thing. In fact, I’d rather display some things that are more relevant for them.
Removing Dashboard Widgets
Dashboard widgets are really just meta boxes, despite them having their own specialized function called wp_add_dashboard_widget
. You can check out all the relevant code for the WP dashboard in wp-admin/includes/dashboard.php
.
To get started we need to hook into wp_dashboard_setup
.
Because dashboard widgets are just meta boxes, we can use the handy remove_meta_box function to get rid of them. Remove meta box, takes three arguments: the widget ID, the page, and the context.
To find those three things, we need to dig into the dashboard code a bit or you can do this:
‘;
exit();
}
[/crayon]
Which will print out the currently registered meta boxes nice and pretty then stop the script. It spits out something like this.
The root key of that array is dashboard
. That’s our page.
The next level down in the array are two more keys: normal
and side
. Those are our contexts!
The next level down are the actual meta box ID strings. Below is the code to remove every meta box from the WordPress admin dashboard. Each one is commented to tell you which widget is being removed. This can be placed in your functions.php
file or in a plugin.
Adding Dashboard Widget
Adding dashboard widgets is just as easy as removing them. Hook into wp_dashboard_setup
again, and call wp_add_dashboard_widget
inside your hooked function. wp_add_dashboard_widget
takes three arguments (four, actually, the last isn’t required): the widget ID, the meta box title, and the callback function.
Your callback function works just like any other meta box: echo out what you need! You also have access to the entire WordPress API inside your widget callback, so you could do things like fetch RSS feeds, or query external API’s and display the data. This is just a simple “Hello, World!” example.