Add custom dashboard widgets to the WordPress admin with PHP

Custom dashboard widgets in the WordPress admin

The admin dashboard is the first screen after login, and on a client site it is mostly WordPress news and “Quick Draft”. A small widget with the numbers the client cares about and links to the screens they use makes it useful. wp_add_dashboard_widget() does it in a few lines.

Problem: The dashboard should show a short site summary (published posts, drafts, pending comments) with quick links, only to users who can edit posts, and without extra queries on every visit.

Solution: Register the widget on the wp_dashboard_setup action with wp_add_dashboard_widget(), check the capability there, cache the counts in a transient, and remove the default widgets you do not need with remove_meta_box().

The widget

add_action( 'wp_dashboard_setup', 'ha_dashboard_widgets' );

function ha_dashboard_widgets() {
    if ( current_user_can( 'edit_posts' ) ) {
        wp_add_dashboard_widget(
            'ha_site_summary',
            __( 'Site summary', 'textdomain' ),
            'ha_site_summary_widget'
        );
    }

    remove_meta_box( 'dashboard_primary', 'dashboard', 'side' );      // WordPress Events and News
    remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );  // Quick Draft
}

function ha_site_summary_widget() {
    $counts = get_transient( 'ha_dashboard_counts' );
    if ( false === $counts ) {
        $posts    = wp_count_posts();
        $comments = wp_count_comments();
        $counts   = [
            'publish'  => (int) $posts->publish,
            'draft'    => (int) $posts->draft,
            'moderate' => (int) $comments->moderated,
        ];
        set_transient( 'ha_dashboard_counts', $counts, 5 * MINUTE_IN_SECONDS );
    }

    echo '<ul>';
    printf( '<li>%s <strong>%d</strong></li>', esc_html__( 'Published posts:', 'textdomain' ), $counts['publish'] );
    printf( '<li>%s <strong>%d</strong></li>', esc_html__( 'Drafts:', 'textdomain' ), $counts['draft'] );
    printf( '<li>%s <strong>%d</strong></li>', esc_html__( 'Comments awaiting moderation:', 'textdomain' ), $counts['moderate'] );
    echo '</ul><p>';
    printf( '<a class="button button-primary" href="%s">%s</a> ', esc_url( admin_url( 'post-new.php' ) ), esc_html__( 'New post', 'textdomain' ) );
    if ( current_user_can( 'moderate_comments' ) ) {
        printf( '<a class="button" href="%s">%s</a>', esc_url( admin_url( 'edit-comments.php?comment_status=moderated' ) ), esc_html__( 'Moderate', 'textdomain' ) );
    }
    echo '</p>';
}

wp_count_posts() and wp_count_comments() are cached by WordPress for the request already; the transient saves the queries across page loads. Five minutes of delay is fine for a summary.

Position and settings

wp_add_dashboard_widget( 'ha_site_summary', __( 'Site summary', 'textdomain' ), 'ha_site_summary_widget', null, null, 'side', 'high' );

The last three arguments (callback arguments, context, priority) were added in WordPress 5.6. The fourth argument is a “configure” callback: WordPress shows a “Configure” link in the widget header, and the callback outputs a form that is posted back to the dashboard.

Users can still drag the widget and hide it in Screen Options; their choice is stored per user and wins over the defaults.

The IDs of the default widgets

dashboard_right_now     At a Glance            normal
dashboard_activity      Activity               normal
dashboard_site_health   Site Health Status     normal
dashboard_quick_press   Quick Draft            side
dashboard_primary       Events and News        side

NOTE: Use a prefixed, unique widget ID: it is also the HTML ID of the box and the key of the user’s layout settings. For notices that must be seen on every admin screen, a widget is the wrong place; see custom admin notices.