Codeable info

Creating WordPress Dashboard Widgets

Posted on by in WordPress Tutorials

Just as widgets can be created to be displayed on the front end of the website, it’s just as easier (or even easier) to create widgets on the WordPress Dashboard. Dashboard widgets, just like regular site widgets, can be used for just about anything. So let’s make a really simple one.

First we write a quick function that will display the contents of our widget:

1
2
3
function wpmods_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>';
}

It won’t show up just yet, so now we need to write a function that will actually create the widget:

1
2
3
function wpmods_add_dashboard_widget() {
	wp_add_dashboard_widget( 'wpmods-custom-widget', 'About WordPress', 'wpmods_dashboard_widget' );
}

The wp_add_dashboard_widget() function has three parameters: widget ID, widget title, and callback function. The widget ID can be anything. The title is entirely up to you, and the callback function is the first function we wrote that displays the content.

Now there’s just one more step to do before our widget will show up.

1
add_action( 'wp_dashboard_setup', 'wpmods_add_dashboard_widget' );

This action will hook our first two functions into WordPress and cause our new widget to show up on the dashboard. If you did everything right, it should look like this:

Enjoy!

)
Codeable info

Comments (8)

Comment by Pippin Williamson says:

Will keep that in mind ;)

Comment by John Flower says:

You see Pippin? Right there. This dude had the same problem as me! I wonder how many others out there struggle with the concept, but don’t voice it. Hmmm, I see a gap for a tutorial or how-to; or a guide: “WordPress’ weird widgetry, and other quirky blog issues.”
:-)

Comment by Webparts Live says:

These comments has solved my query on the same. i was stuck with the same problem like John’s. Thanks for help.

Comment by John Flower says:

Dude, you really think that description makes this easier to understand? ;-P What about if someone is a complete total absolute WordPress noob (not unlike what I almost am)?

Comment by Pippin Williamson says:

The thing is John, these default widgets aren’t exactly widgets. Not in the WordPress widgets sense anyway. They are simply things the developer has hard coded into the theme to look like widgets and to take up the place of widgets when they’re not there.

Comment by John Flower says:

Thanks for the reply Pippin. This wouldn’t be a problem if these default widgets at least showed up as active in the back office. It sucks.

Comment by Pippin Williamson says:

So what is happening here is that the theme author has set it up so that default widgets get displayed if you don’t have any widgets set in the admin. This prevents large blank spaces from showing up in your site. The moment you place a widget through the admin, it replaces the default widgets with whatever you have chosen.

Without directly editing the theme template files, there’s not a way to disable to default widgets without placing a widget through the admin.

Comment by John Flower says:

I just cannot get my head around widgets. For instance, whenever you install a theme, you’ll see a widget or widgets in the sidebar. When you go to the widgets area in the back office, the widgets aren’t showing in the areas that the site is claiming they’re placed in. But if you add a widget to a sidebar area, it removes the current widgets.
Furthermore, how do you disable a widget that doesn’t show (in the back office)? There’s nothing there to remove, according to the back office, but the website shows the widget.

what am I missing?

Codeable info