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.

WordCamp Chicago

I’m speaking at WordCamp Chicago this weekend at the Chicago Mart Plaza. My session is titled Beginners Plugin Development and it will be an introduction to writing a WordPress plugin. WordCamp ChicagoYou can attend my session on Sunday, June 6th at 11:30am 11:15am in the Developers Track.

WordCamp Chicago has an exciting line-up of speakers and I’m looking forward to meeting with Chicagoland WordPress people and reconnecting with some folks I met at WordCamp San Francisco. WordCamps are all about networking, so make it a plan to attend all the parties and hit me up if you see me.

WordCamp San Francisco

WordCamp San FranciscoI’m attending WordCamp San Francisco this weekend at Mission Bay Conference Center. WordCamp San Francisco is the original WordCamp and is generally considered the best gathering of the WordPress geeks. You should definitely check it out if you are in the area.

WordCamps are all about networking, so make it a plan to attend all the parties and hit me up if you see me. WordCamp San Francisco has an exciting lineup of speakers and it’s well worth the $50 in addition to a cool t-shirt and a swag bag.

It’s officially a one day conference but there is a developer unconference scheduled for Sunday, May 2nd at the Automattic Lounge (Pier 38) if development is your forte. There is also a code sprint scheduled for May 3rd and 4th and everyone is welcome to attend if you are interested in contributing to WordPress.