3

I recently tried to reorder the dashboard widgets by writing a plugin. I accomplished this:

enter image description here

with the following code:

<?php
/*
 * Plugin Name: Custom Dashboard
 * Description: Custom dashboard for Avare sites.
 * Author: Avare
 * Version: 1.0
 */

function sort_dashboard_widgets() {

 $left_column_widgets[] = 'dashboard_right_now';
 $left_column_widgets[] = 'dashboard_recent_comments';
 $left_column_widgets[] = 'dashboard_incoming_links';

 $right_column_widgets[] = 'dashboard_quick_press';
 $right_column_widgets[] = 'dashboard_recent_drafts';
 $right_column_widgets[] = 'dashboard_primary';
 $right_column_widgets[] = 'dashboard_secundary';

// Global the $wp_meta_boxes variable (this will allow us to alter the array)
global $wp_meta_boxes;

// We then unset that part of the array
unset($wp_meta_boxes['dashboard']['normal']['core']);
unset($wp_meta_boxes['dashboard']['side']['core']);

// Then we make a backup of the widget areas
$left_dashboard = $wp_meta_boxes['dashboard']['normal']['core'];
$right_dashboard = $wp_meta_boxes['dashboard']['side']['core'];

// Then we merge them in some sort of way (is this necessary?)
$sorted_left_dashboard = array_merge((array)$left_column_widgets, (array)$left_dashboard);
$sorted_right_dashboard = array_merge((array)$right_column_widgets, (array)$right_dashboard);

// Now we add the sorted widgets back in
$wp_meta_boxes['dashboard']['normal']['core'] = $sorted_left_dashboard;
$wp_meta_boxes['dashboard']['side']['core'] = $sorted_right_dashboard;

}

add_filter('wp_dashboard_setup', 'sort_dashboard_widgets');

As you can see in the screenshot I made, it looks like the plugin only looks at the first letter of $left_column_widgets and $right_column_widgets, which causes the errors (because 'd' is not a valid widget slug). Is there any way to solve this problem?

Cheers

2 Answers 2

2

Clearing things up

First, there's a slight misunderstanding. wp_dashboard_setup is an action and not a filter. If it would be a filter, it would have one or multiple arguments and would need to return the first one.

How-To #1

For an example of this action, see the following mu-plugin I use:

<?php
/**
 * Plugin Name: Remove Dashboard Widgets
 * Description: Removes all Dashboard Widgets
 */
function dashboard_widgets()
{
    remove_meta_box( 'dashboard_browser_nag',     'dashboard', 'normal' );
    remove_meta_box( 'dashboard_right_now',       'dashboard', 'normal' );
    remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
    remove_meta_box( 'dashboard_plugins',         'dashboard', 'normal' );

    remove_meta_box( 'dashboard_quick_press',     'dashboard', 'side' );
    remove_meta_box( 'dashboard_recent_drafts',   'dashboard', 'side' );
    remove_meta_box( 'dashboard_primary',         'dashboard', 'side' );
    remove_meta_box( 'dashboard_secondary',       'dashboard', 'side' );
}
add_action( 'wp_dashboard_setup', 'dashboard_widgets' );

After removing those widgets, you can add them back in using

add_dashboard_widget( $widget_id, $widget_name, $callback );

For more info consult the Codex and the Dashboard Widgets API. To see the defaults, look into /wp-admin/includes/dashboard.php -> wp_setup_dashboard(). Then simply add them back in as you like/need them.

How-To #2

There're also different other options you got: Filters.

But first we should clear up which dashboard you're targeting. All those will have in some cases (plugins, etc.) different dashboards and different filters

  • Network Administrators (a.k.a. Super-Admins) -> wp_network_dashboard_widgets
  • Site Administrator -> wp_user_dashboard_widgets
  • User -> wp_dashboard_widgets

All those filters filter the $dashboard_widgets array (and should return it).

Extending the defaults

If you want to extend the default Widgets, you will want to use the 'do_meta_boxes' hook, which has three arguments (Screen ID, location like side, normal, etc. and an empty third argument.

Drawbacks and limits

You'll have to accept the fact that users are able to reorder those widgets (or even deactivate them). This is something that should be that way and kept like this.

4
  • 1
    Thank you for your awesome reply :) during the day I figured out myself how to remove the meta boxes from the dashboard and how to add them in the order I want them to. This is the code I wrote: codedump.nl/code/950485. I know that the users will be able to reorder them, but I wanted to determine the standard order of the widgets :) I didn't know that I had to use add_action instead of add_filter, thanks for that ^^
    – Steef
    Commented Jan 20, 2013 at 17:21
  • @user26264 +1 for figuring it out. You can do still: some things here. A) Add the code as a separate answer so later users will also profit from it. B) Use a filter instead of intercepting the global. C) Mark your own answer as the solution (and upvote my answer ;) ).
    – kaiser
    Commented Jan 20, 2013 at 19:56
  • Shall do, thanks. :) But why shouldn't I use global? If I would use a filter instead of global, I have to manually remove the widgets I don't want to appear on the dashboard.
    – Steef
    Commented Jan 20, 2013 at 20:00
1

Whoop, I figured it out. :) Although I figured it out before kaiser answered my question, I would still like to thank him for is clear and awesome answer. Below you'll find the code I wrote to reorder the dashboard widgets.

<?php
/**
 * Plugin Name: Custom Dashboard
 * Description: Custom dashboard for Avare sites.
 * Author: Avare
 * Version: 1.0
 */

function sort_dashboard_widgets() {

    // Global the $wp_meta_boxes variable (this will allow us to alter the array)
    global $wp_meta_boxes;

    // Then unset everything in the array
    unset($wp_meta_boxes['dashboard']['normal']['core']);
    unset($wp_meta_boxes['dashboard']['side']['core']);

    // Add widgets to left column
    add_meta_box('dashboard_right_now', __('Right Now'), 'wp_dashboard_right_now', 'dashboard', 'normal', 'core');
    add_meta_box('dashboard_recent_comments', __('Recent Comments'), 'wp_dashboard_recent_comments', 'dashboard', 'normal', 'core');

    // Add widgets to right column
    add_meta_box('dashboard_quick_press', __('QuickPress'), 'wp_dashboard_quick_press', 'dashboard', 'side', 'core');
    add_meta_box('dashboard_recent_drafts', __('Recent Drafts'), 'wp_dashboard_recent_drafts', 'dashboard', 'side', 'core');

}

add_action('wp_dashboard_setup', 'sort_dashboard_widgets');

Please note that this code doesn't use all the available dashboard widgets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.