Halloween Sale 15% off Ends in See the plans
PHP

Daily Digest of Comments Awaiting Moderation

Replaces the one-email-per-comment moderation notices with a single daily email listing every comment waiting for approval, with a link to the moderation screen.

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

Code

$wpide_digest_hour = 8; // Hour of the day (0-23, site time).

add_filter( 'notify_moderator', '__return_false' );

add_action( 'init', function () use ( $wpide_digest_hour ) {
    if ( wp_next_scheduled( 'wpide_pending_comments_digest' ) ) {
        return;
    }
    $next = new DateTime( 'today ' . $wpide_digest_hour . ':00', wp_timezone() );
    if ( $next->getTimestamp() <= time() ) {
        $next->modify( '+1 day' );
    }
    wp_schedule_event( $next->getTimestamp(), 'daily', 'wpide_pending_comments_digest' );
} );

add_action( 'wpide_pending_comments_digest', function () {
    $comments = get_comments( array( 'status' => 'hold', 'type' => 'comment', 'number' => 50, 'orderby' => 'comment_date_gmt', 'order' => 'DESC' ) );

    if ( ! $comments ) {
        return;
    }

    $total = (int) get_comments( array( 'status' => 'hold', 'type' => 'comment', 'count' => true ) );
    $lines = array( sprintf( '%d comment(s) are waiting for your approval:', $total ), '' );

    foreach ( $comments as $comment ) {
        $lines[] = sprintf( '• %s on "%s": %s', $comment->comment_author, wp_strip_all_tags( get_the_title( $comment->comment_post_ID ) ), wp_html_excerpt( wp_strip_all_tags( $comment->comment_content ), 120, '…' ) );
    }

    $lines[] = '';
    $lines[] = 'Moderate them: ' . admin_url( 'edit-comments.php?comment_status=moderated' );

    wp_mail( get_option( 'admin_email' ), sprintf( '[%s] %d comment(s) awaiting moderation', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), $total ), implode( "\n", $lines ) );
} );

Notes

Sent at the hour set at the top (site time) only when comments are waiting; goes to the admin email address.

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.