The wp_dashboard_setup action in WordPress allows users to customize the dashboard, which is the first screen that users see when they log into the WordPress admin area. This action is triggered after the core dashboard widgets have been registered and allows users to add, remove, or rearrange widgets in the dashboard.

To use the wp_dashboard_setup action, users can write a custom function that hooks into the action and specifies the desired changes to the dashboard. For example, to remove a dashboard widget, users can use the remove_meta_boxfunction:

function remove_dashboard_widgets() {
    remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
}
add_action( 'wp_dashboard_setup', 'remove_dashboard_widgets' );

To add a new dashboard widget, users can use the wp_add_dashboard_widget function:

function add_custom_dashboard_widget() {
    wp_add_dashboard_widget( 'custom_dashboard_widget', 'Custom Dashboard Widget', 'display_custom_dashboard_widget' );
}
add_action( 'wp_dashboard_setup', 'add_custom_dashboard_widget' );

The wp_add_dashboard_widget function takes three arguments: a unique ID for the widget, a title for the widget, and a callback function that displays the content of the widget.

Users can also rearrange the order of dashboard widgets by using the wp_dashboard_setup action in combination with the set_meta_box_order function. For example:

function rearrange_dashboard_widgets() {
    $meta_boxes = array(
        'dashboard_incoming_links' => 'normal',
        'dashboard_right_now' => 'normal',
        'custom_dashboard_widget' => 'side',
    );
    set_meta_box_order( 'dashboard', $meta_boxes );
}
add_action( 'wp_dashboard_setup', 'rearrange_dashboard_widgets' );

The set_meta_box_order function takes two arguments: the screen ID and an array of widgets and their positions (side or normal).

By using the wp_dashboard_setup action and the related functions, users can customize the dashboard to better meet their needs and preferences. This can be particularly useful for websites with multiple authors or for websites that need to display specific information to their users.

In addition to adding, removing, and rearranging widgets, users can also customize the appearance and behavior of existing dashboard widgets using the wp_dashboard_setup action. For example, users can use the wp_dashboard_recent_posts_query_args filter to customize the query arguments for the “Recent Posts” widget:

function customize_recent_posts_widget_query( $query_args ) {
    $query_args['category__in'] = array( 1, 2 );
    return $query_args;
}
add_filter( 'wp_dashboard_recent_posts_query_args', 'customize_recent_posts_widget_query' );

Users can also use the dashboard_glance_items filter to customize the items displayed in the “At a Glance” widget:

function customize_glance_items( $items ) {
    $custom_post_types = array( 'book', 'movie' );
    foreach ( $custom_post_types as $post_type ) {
        $num_posts = wp_count_posts( $post_type );
        if ( $num_posts && $num_posts->publish ) {
            $published_posts = number_format_i18n( $num_posts->publish );
            $post_type_object = get_post_type_object( $post_type );
            $text = _n( '%s ' . $post_type_object->labels->singular_name, '%s ' . $post_type_object->labels->name, $published_posts );
            $items[] = sprintf( $text, $published_posts );
        }
    }
    return $items;
}
add_filter( 'dashboard_glance_items', 'customize_glance_items' );

By using filters and custom functions, users can tailor the dashboard to meet the specific needs and goals of their website.

In General, the wp_dashboard_setup action is a powerful tool for customizing the WordPress dashboard. By adding, removing, rearranging, and customizing widgets, users can create a dashboard that is tailored to their specific needs and goals.
Good luck trying it out !

Categorized in: