Finally WordPress 2.7 is coming along with widgets in the dashboard area, so it will be possible to adjust your dashboard as you like.
For Plugin or theme authors it’s even more interesting, since they can give the users additional value to their Plugins. They can offer information from their Plugin directly on the dashboard. The user can decide if he likes to display them or not.
It’s easy to integrate and can be controlled like Meta Boxes (explained in detail). The following example shows how you can integrate a widget in a dashboard.

The above screenshot shows that I addes the widget Test My Dashboard. Also you can see the possibility to deactivate the widget in the menu.
/**
* Content of Dashboard-Widget
*/
function my_wp_dashboard_test() {
echo 'Test Add Dashboard-Widget';
}
/**
* add Dashboard Widget via function wp_add_dashboard_widget()
*/
function my_wp_dashboard_setup() {
wp_add_dashboard_widget( 'my_wp_dashboard_test', __( 'Test My Dashboard' ), 'my_wp_dashboard_test' );
}
/**
* use hook, to integrate new widget
*/
add_action('wp_dashboard_setup', 'my_wp_dashboard_setup');
The function wp_add_dashboard_widget()
is the key, which delivers the new widget; via hook wp_dashboard_setup
it will be activated on the dashboard.
function wp_add_dashboard_widget( $widget_id, $widget_name, $callback, $control_callback = null )
The function needs 3 parameters and an additional one is optional.
- The ID, first parameter,will be used to deactivate the widget via JavaScript. In the Menu:
and also in the widget:
Test My Dashboard
Test Add Dashboard-Widget - The second parameter delivers the name, which will be displayed, wrapped in an h3-tag.
Test My Dashboard
- With the help of the third parameter the third function gets delivered, it contains the content of the widget. The
echo
does not take place, you have to use it explicitly. Therefore not only the content can be displayed, which makes it more flexible.
Hint
The hook activity_box_end
, which is available since version 2.3, still exists. It adds the content in widget „Right Now“.
Update
Nice to read: a update of this post to include options in the dashboard widget: How add options to your WordPress 2.7 dashboard widgets
Comments
36 responses to “Add WordPress Dashboard Widgets”
Thanks so much for this!
Works perfectly.
Excellent bit of information
We are happy it’s working out for you just perfectly 🙂
hi, I’ve got a plugin like WP e-commerce that create a widget on dasboard, but it doesn’t work with 2.7…
In fact, widget is integrated in the “right now” widget.
I’m french, I don’t know if I’m understood, so I made a print screen :
http://img392.imageshack.us/img392/9097/tableau03ya4.gif
Can you help me?
Great info!
Just one question: How can we add the ‘Configure widget’ feature?
Thanks!
Rick, have the widget use a function with your stuff ofcourse and put the settings for it in the settings panel
Rick, you can add a link directly in the h3-tag to your settings. That should work.
I mean the feature, not the link 🙂
Involves a 4th(and optional) argument in the wp_add_dashboard_widget(), but I’m still investigating 🙂
Rick is correct. The 4th parameter calls a function to display configuration options. WP will automatically add the link in the corner to pull this up.
I traced it back to this function:
s/wp-admin/includes/dashboard.php
function _wp_dashboard_control_callback( $dashboard, $meta_box ) {
echo '';
wp_dashboard_trigger_widget_control( $meta_box['id'] );
echo "";
echo '';
}
then get a bit lost when it gets to here:
wp_dashboard_trigger_widget_control( $meta_box['id'] );
Any help? I just want to give a couple of options to set. I’d rather not create a page under settings for this when the option to set it right there is available.
Thanks in Advance…
Adam
Adam, check this:
http://rick.jinlabs.com/2009/02/01/how-add-options-to-your-wordpress-27-dashboard-widgets/
I am trying to create a simple widget with a control panel. Everything is working except for when user enters website address in control panel, when link is clicked on in the theme it just refreshes the page instead of taking user to the website. I have created my widgets in a file called ‘widgets.php’ and all work fine, just need to figure out how to get user enetered links to work. You can see the code below:
` function twitterWidget()
{
$settings = get_option(“widget_twitterwidget”);
$title = $settings[‘title’];
$twiturl = $settings[‘twiturl’];
?>
<img src=”” height=”16″ width=”16″ alt=””/> <a href=””>
<?php
}
// Twitter Widget
function twitterWidgetAdmin() {
$settings = get_option(“widget_twitterwidget”);
// check if anything’s been sent
if (isset($_POST[‘update_twitter’])) {
$settings[‘title’] = strip_tags(stripslashes($_POST[‘twitter_title’]));
$settings[‘twiturl’] = strip_tags(stripslashes($_POST[‘twitter_twiturl’]));
update_option(“widget_twitterwidget”,$settings);
}
echo ‘
Title:
‘;
echo ‘
Twitter URL:
‘;
echo ”;
}
register_sidebar_widget(‘Twitter’, ‘twitterWidget’);
register_widget_control(‘Twitter’, ‘twitterWidgetAdmin’, 400, 200);`
Any help would be greatly appreciated.
Thanks
How add options to your WordPress 2.7 dashboard widgets…
Well, in this article, I assume you have read the WP Engineer’s excellent post Add WordPress Dashboard Widgets, because his code will be our start point.
So, we have this code:
// Load up the localization file if we’re using WordPress i…
How could i even over look the obvious. Damn, musta been asleep that day.
Thanks!!
Adam
Thanks so much for this!
Can anyone provide a snippet for hiding/showing a dashboard widget based on user roles?
Thanks.
@Mark: small snippet with example, i hope you understand this.
Thanks Frank. I separated out the widget into an include and used the following code within the plugin. Maybe I should explain what I was doing. I found a plugin to create a To-Do list and wanted the list shown in the dashboard when users with particular roles logged in. This is what I ended up with:
// ——————————————————————–
// Dashboard widget
// ——————————————————————–
//Pre-check user status and role and show only to allowed roles
function hide_show_assignboard(){
if(is_user_logged_in) { // only if there is a user logged in
require (ABSPATH . WPINC . ‘/pluggable.php’);
$logged_user = wp_get_current_user();
$user_roles = $logged_user->roles; // an array of roles
foreach($user_roles as $user_role)
{
if($user_role==’administrator’ || $user_role==’intern’ ||$user_role==’editor’)
include(‘dashboard.php’);}
}
}
hide_show_assignboard();
I discovered late last night that I had to use this line to prevent an error that had me pulling out my hair:
require (ABSPATH . WPINC . ‘/pluggable.php’);
Would your code be placed in the widget? I could send you an archive of the modified plugin.
Hi how can I login to wordpress dashboard and to Design-> Widgets on my computer? I want to transfer a donate button code to my website To create a donate button but I don’t how. Could you tell me how.
Thanks
There’s Paypal donate plugin at http://wordpress.org/extend/plugins/wordpress-paypal-donations-plugin/
You could search the WP plugins and widgets databases for others. Why reinvent the wheel?
If you want to create a sidebar widget with a donate button, you could use the example widget code at WP and insert your Paypal button code.
Seems that, i do not get any “visible” options at the Screen option menu… Seems that it does not pass there… And i have had just copy pasted the code 😕
Really Great..
I was searching for ‘wordpress dashboard widget’, and found your post. This saved me a lot.
I have been looking at removing the dashboard widgets in WP 2.8.4. This is my plugin code, you should just be able to add the id of the widget you want to remove.
add_action(‘do_meta_boxes’, array(&$this, ‘do_meta_boxes’),10,3);
function do_meta_boxes($type, $context, $post)
{
remove_meta_box(‘dashboard_quick_press’, $type, $context);
}
This code works perfectly for WordPress single installation, but if you activate it site wide on WPMU it only adds the new dashboard widget in the main blog dashboard… 🙁
Anyone knows to make the widget to add on every blog dashboard of a WPMU installation?
Hey Samuel, moving the plugin to mu-plugins solves the problem.
Hi Alex,
Do you know if i can choose the place of my widget with this function wp_add_dashboard_widget ?
Is it possible to specify that the widget must be in fisrt position to the top of the first column for example ?
with this hack is possible to put a widget text (that configured in appearance) in the dashboard? thanks
im trying to add a text widget of widgets sidebar in dashboard. how can i do it? anyone can help me, please!!
Marujo > paste this in function.php
<?php
if (!function_exists('text_widget')) {
function text_widget() {
echo 'Text widget by ARCHIparmentier | wordpressdesigner’;
}
function archi_setup() {
wp_add_dashboard_widget( ‘text_widget’ , ‘text widget’ , ‘text_widget’);
}
add_action(‘wp_dashboard_setup’, ‘archi_setup’);
}
?>
hi sebastien. i try but only appears a blank widget. i need to show the editable content in the text widget also in dashboard. it is possible? i notice that the theme im usiong the sidebar as named as ‘right’, and the name of my text widget is ‘Cotação Câmbio”. if you can help me, i thank you a lot. thanks!
If you use my code, a widget appears in the dashboard.
In this widget you can read “Text widget by ARCHIparmentier | wordpressdesigner’”. This text is product by the function text_widget()
You can change this function to product another result.
Is there a way to add a new widget to the dashboard with ajax? After the dashboard finished loading?
Hi Frank,
Thanks for a good article.
Do you know of a built in function to add a widget to the side dashboard? The wp_add_dashboard_widget function seems to only allow adding it to the ‘normal’ area.
I can add the fields directly to the $wp_meta_boxes array, but a built in function would be much more robust. 🙂
I’d like to echo marujo’s question about adding text widgets to the dashboard.
I’ve tried blending these snippets and those from Rick’s follow up article (http://rick.jinlabs.com/2009/02/01/how-add-options-to-your-wordpress-27-dashboard-widgets/) with snippets from the default-widgets.php or dashboard.php or posts like (http://justintadlock.com/archives/2009/05/26/the-complete-guide-to-creating-widgets-in-wordpress-28) but can’t seem to manage any output to my sidebar.
This should be possible yes? Any ideas?
I think this could be very interesting for providing easier-to-use widgets for the user client.
Thanks for sharing your expertise.
Best,
Owen
[…] Information can be found here: Removing Dashboard Widgets WordPress Codex: Admin Widgets API Help Adding Widgets Tutorial 10 Dashboard Hacks Google Analytics Dashboard Integration Email Dashboard Widget Facebook Dashboard […]
Great post and it’s still applicable after all this time.
For anyone interested, I just posted this article and ensuing form to automatically create dashboard widgets. It’s a javascripted form that creates all the necessary plugin code from the input parameters. I’ve tested it myself and it’s worked fine for including a Google Gadget, which I think has great potential. Anyway, it’s at the following.
http://thehacked.com/wordpress-dashboard-widget-plugin-maker/
Enjoy,
DavidM