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.

Prevent Update Checks for a WordPress Plugin

Earlier today, Jason Gegere asked me how to prevent WordPress from checking for updates on a plugin. This may be helpful if you are running a custom version of a plugin from the repository and you don’t want to accidently update the plugin and lose the customizations, or if you are running a private plugin and don’t want WordPress to check for updates from the plugin repository. WordPress doesn’t know if your plugin is in the WordPress plugins repository and it will request the update check on all installed plugins.

WordPress checks for updates using wp_update_plugins() in wp-includes/update.php but there are no hooks in this function which could be used to exclude your plugin from the update check. However, you can hook into http_request_args and remove your plugin from the query arguments passed in the http request. You can add the following code to your plugin to prevent WordPress from checking for updates for your plugin:

add_filter( 'http_request_args', 'dm_prevent_update_check', 10, 2 );
function dm_prevent_update_check( $r, $url ) {
	if ( 0 === strpos( $url, 'http://api.wordpress.org/plugins/update-check/' ) ) {
		$my_plugin = plugin_basename( __FILE__ );
		$plugins = unserialize( $r['body']['plugins'] );
		unset( $plugins->plugins[$my_plugin] );
		unset( $plugins->active[array_search( $my_plugin, $plugins->active )] );
		$r['body']['plugins'] = serialize( $plugins );
	}
	return $r;
}

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.