Reorder dashboard widgets

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

enter image description here

Read More

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

Related posts

Leave a Reply

2 comments

  1. 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.

  2. 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.