New Sign-Ups per Month Dashboard Widget
Adds a dashboard widget showing how many people registered in each of the last six months, with simple bars, so you can see if your community is growing.
Opens WPIDE on
Code
add_action( 'wp_dashboard_setup', function () {
if ( ! current_user_can( 'list_users' ) ) {
return;
}
wp_add_dashboard_widget( 'wpide_signups_month', 'New sign-ups per month', function () {
global $wpdb;
$rows = get_transient( 'wpide_signups_month' );
if ( false === $rows ) {
$since = gmdate( 'Y-m-01 00:00:00', strtotime( '-5 months', time() ) );
$rows = $wpdb->get_results( $wpdb->prepare( "SELECT DATE_FORMAT(user_registered, '%%Y-%%m') AS month, COUNT(*) AS total FROM {$wpdb->users} WHERE user_registered >= %s GROUP BY month ORDER BY month", $since ), ARRAY_A );
set_transient( 'wpide_signups_month', $rows, 6 * HOUR_IN_SECONDS );
}
$totals = wp_list_pluck( (array) $rows, 'total', 'month' );
$max = $totals ? max( array_map( 'intval', $totals ) ) : 0;
echo '<table class="widefat striped" style="border:0"><tbody>';
for ( $i = 5; $i >= 0; $i-- ) {
$time = strtotime( gmdate( 'Y-m-01' ) . " -$i months" );
$count = (int) ( $totals[ gmdate( 'Y-m', $time ) ] ?? 0 );
$width = $max ? round( $count / $max * 100 ) : 0;
printf(
'<tr><th scope="row" style="width:30%%">%1$s</th><td><span style="display:inline-block;height:10px;width:%2$d%%;min-width:2px;background:#2271b1;border-radius:2px;vertical-align:middle"></span> %3$s</td></tr>',
esc_html( wp_date( 'M Y', $time ) ),
(int) $width,
esc_html( number_format_i18n( $count ) )
);
}
echo '</tbody></table>';
} );
} );
add_action( 'user_register', function () {
delete_transient( 'wpide_signups_month' );
} );
Notes
Uses one grouped query, cached for 6 hours. Visible to users who can list users.
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.