Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.