Delete Custom Dashboard Widgets

The built-in dashboard widgets are not marked somehow, you have to use a fixed list:

'dashboard_right_now',
'dashboard_plugins',
'dashboard_quick_press',
'dashboard_recent_drafts',
'dashboard_recent_comments',
'dashboard_incoming_links',
'dashboard_primary',
'dashboard_secondary'

So your code should test if the current widget is in that list and remove the widget, if it isn’t:

add_action(
    'wp_dashboard_setup',
    't5_remove_custom_dashboard_widgets',
    11
);

function t5_remove_custom_dashboard_widgets()
{
    global $wp_meta_boxes;

    $builtin = array (
        'dashboard_right_now',
        'dashboard_plugins',
        'dashboard_quick_press',
        'dashboard_recent_drafts',
        'dashboard_recent_comments',
        'dashboard_incoming_links',
        'dashboard_primary',
        'dashboard_secondary'
    );

    if ( empty ( $wp_meta_boxes['dashboard'] ) )
        return;

    $widget_groups = $wp_meta_boxes['dashboard'];

    foreach ( $widget_groups as $section => $widget_group )
        foreach ( $widget_group as $widgets )
            foreach ( $widgets as $id => $widget )
                if ( ! in_array( $id, $builtin ) )
                    remove_meta_box( $id, 'dashboard', $section );
}

Leave a Comment