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.
Opens WPIDE on
Code
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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.