How to Create a WordPress Dashboard Widget

This post is the first in a series that was inspired by my WordPress Chicago presentation titled Beginner’s Plugin Development.

A new Dashboard Widgets API was introduced in WordPress 2.7, which made it easy for developers to create their own widgets for the administration dashboard. Dashboard Widgets are written in PHP and it’s easy to get started if you are familiar with the WordPress Plugin API.

You can add a new widget to the dashboard using the WordPress core function wp_add_dashboard_widget( widget_id, widget_name, callback, control_callback ).

  • widget_id: Unique id for your widget. This will be the css class and the array key for the widgets.
  • widget_name: Name for your widget that will be displayed as the title.
  • callback: The function that will display the content of the widget.
  • control_callback: The function that will display the form elements and handle the form submission. This parameter is optional.

In this post, I’ll create a simple dashboard widget that will display some text:

Let’s create a function that will display the contents of our custom dashboard widget:

function devmind_dashboard_widget() {
	echo '<p><a href="http://wordpress.org/">WordPress</a> is a state-of-the-art publishing platform with a focus on aesthetics, web standards, and usability. WordPress is both free and priceless at the same time.</p>';
}

We need to create another function that will call wp_add_dashboard_widget and register our custom dashboard widget:

function devmind_add_dashboard_widget() {
	wp_add_dashboard_widget( 'devmind-custom-widget', 'About WordPress', 'devmind_dashboard_widget' );
}

Now, we need to hook into dashboard setup process and tell WordPress to load our widget. We can do this by hooking into wp_dashboard_setup and adding our custom widget.

add_action( 'wp_dashboard_setup', 'devmind_add_dashboard_widget' );

You can add this code to your theme’s functions.php file or download it as a plugin here.