Halloween Sale 15% off Ends in See the plans
PHP

Weekly Site Activity Summary by Email

Emails the site admin every Monday with last week's numbers: new posts, comments, pending items and new users, so you keep an eye on the site without logging in.

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

Code

$wpide_summary_hour       = 9;
$wpide_summary_recipients = array(); // Extra addresses; the admin email is always included.

add_action( 'init', function () use ( $wpide_summary_hour ) {
    if ( wp_next_scheduled( 'wpide_weekly_summary' ) ) {
        return;
    }
    $next = new DateTime( 'monday this week ' . $wpide_summary_hour . ':00', wp_timezone() );
    if ( $next->getTimestamp() <= time() ) {
        $next->modify( '+1 week' );
    }
    wp_schedule_event( $next->getTimestamp(), 'weekly', 'wpide_weekly_summary' );
} );

add_action( 'wpide_weekly_summary', function () use ( $wpide_summary_recipients ) {
    $since = gmdate( 'Y-m-d H:i:s', time() - WEEK_IN_SECONDS );

    $posts = new WP_Query( array( 'post_type' => 'post', 'post_status' => 'publish', 'date_query' => array( array( 'after' => $since, 'column' => 'post_date_gmt' ) ), 'fields' => 'ids', 'posts_per_page' => 1 ) );
    $users = new WP_User_Query( array( 'date_query' => array( array( 'after' => $since, 'column' => 'user_registered' ) ), 'fields' => 'ID', 'number' => 1, 'count_total' => true ) );

    $stats = array(
        'Posts published'             => (int) $posts->found_posts,
        'Comments approved'           => (int) get_comments( array( 'status' => 'approve', 'count' => true, 'date_query' => array( array( 'after' => $since, 'column' => 'comment_date_gmt' ) ) ) ),
        'Comments waiting'            => (int) wp_count_comments()->moderated,
        'Posts waiting for review'    => (int) wp_count_posts( 'post' )->pending,
        'New users'                   => (int) $users->get_total(),
    );

    $lines = array( 'Here is what happened on ' . home_url() . ' in the last 7 days:', '' );
    foreach ( $stats as $label => $value ) {
        $lines[] = sprintf( '%-28s %d', $label . ':', $value );
    }
    $lines[] = '';
    $lines[] = 'Dashboard: ' . admin_url();

    $to = array_merge( array( get_option( 'admin_email' ) ), array_filter( $wpide_summary_recipients, 'is_email' ) );
    wp_mail( $to, sprintf( '[%s] Weekly summary', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) ), implode( "\n", $lines ) );
} );

Notes

Sent on Monday at the hour set at the top (site time); add more recipients to the list.

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.