# Comments per Day Chart Widget

> Adds a dashboard widget with a simple bar chart of approved comments for each of the last 30 days, so you can see at a glance whether reader engagement is growing.

- Type: PHP
- Runs: Admin only
- Category: [Dashboard](https://wpide.com/library/category/dashboard/)
- Tags: dashboard-widget, comments, chart
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/dash-comments-trend-chart/

## Code

```php
add_action( 'wp_dashboard_setup', function () {
    if ( ! current_user_can( 'moderate_comments' ) ) {
        return;
    }

    wp_add_dashboard_widget( 'wpide_comments_trend', 'Comments in the last 30 days', function () {
        global $wpdb;

        $rows = get_transient( 'wpide_comments_trend' );
        if ( false === $rows ) {
            $rows = $wpdb->get_results( $wpdb->prepare(
                "SELECT DATE(comment_date) AS day, COUNT(*) AS total FROM {$wpdb->comments}
                WHERE comment_approved = '1' AND comment_type IN ('comment','') AND comment_date >= %s GROUP BY day",
                wp_date( 'Y-m-d 00:00:00', strtotime( '-29 days', current_time( 'timestamp' ) ) )
            ), OBJECT_K );
            set_transient( 'wpide_comments_trend', $rows, HOUR_IN_SECONDS );
        }

        $days = array();
        for ( $i = 29; $i >= 0; $i-- ) {
            $day          = wp_date( 'Y-m-d', strtotime( "-$i days", current_time( 'timestamp' ) ) );
            $days[ $day ] = isset( $rows[ $day ] ) ? (int) $rows[ $day ]->total : 0;
        }
        $max   = max( 1, max( $days ) );
        $total = array_sum( $days );

        printf( '<p><strong>%s</strong> comments in 30 days</p>', esc_html( number_format_i18n( $total ) ) );
        echo '<div style="display:flex;align-items:flex-end;gap:2px;height:80px" role="img" aria-label="' . esc_attr( sprintf( '%d comments in the last 30 days', $total ) ) . '">';
        foreach ( $days as $day => $count ) {
            printf(
                '<div title="%s: %d" style="flex:1;height:%d%%;min-height:2px;background:%s;border-radius:2px 2px 0 0"></div>',
                esc_attr( mysql2date( get_option( 'date_format' ), $day ) ),
                (int) $count,
                (int) round( $count / $max * 100 ),
                $count ? '#2271b1' : '#dcdcde'
            );
        }
        echo '</div>';
    } );
} );
```

## Notes

Pure HTML and CSS; data is cached for an hour.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Comments per Day Chart Widget" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
