Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

208 PART III • Using Plugins with WordPress


Hey you! Don\'t forget the internal pages for important stuff:<br />
← <a href="http://domain.com/internal/forum">Forum</a><br />
← <a href="http://domain.com/internal/docs">Documentation</a><br />
← <a href="http://domain.com/internal/staff">Staff</a><br />
OK THX BYE!
';
}

It’s a simple function called dashboard_reminder() that echoes some HTML. This is
what you want the widget to display. The next step is to add the Dashboard widget itself:

function dashboard_reminder_setup() {
wp_add_dashboard_widget( 'dashboard_reminder_widget', 'Staff Reminder',
'dashboard_reminder' );
}

The key here is wp_add_dashboard_widget(), to which you pass first an identifying ID
of the widget ('dashboard_reminder_widget' in this case), then the text label to
describe the widget, and finally the name of the function containing the Dashboard widget’s
content (obviously, the dashboard_reminder() function). It’s worth knowing that the
Dashboard widget ID, which is the first parameter passed to wp_add_dashboard_
widget(), is also the class the widget will get should you want some fancy CSS stylings.

Pause for a moment and look at the wp_add_dashboard_widget() function. There’s a
fourth parameter as well called $control_callback, which passes a null value by default
and is optional. It isn’t included in this example, but it may be a good idea to keep in mind
that you can pass a fourth parameter for more functionality in your Dashboard widget.

Returning to the example, you need to add the widget action to the wp_dashboard_setup
hook, using add_action():

add_action( 'wp_dashboard_setup', 'dashboard_reminder_setup' );

There you have it, a Dashboard widget (see Figure 7-10)! As of now, there is no Dashboard
widget order API, so your widget will end up at the bottom in the Dashboard. The user can
reorder widgets manually, of course, but you may want to force things to the top. There are
some hacks available online, such as the one described in the WordPress Codex (http://
codex.wordpress.org/Dashboard_Widgets_API), should you need to achieve this.

Figure 7-10: The Dashboard widget in all its glory.
Free download pdf