Halloween Sale 15% off Ends in See the plans
PHP

Email Subscribers When a New Post Is Published

Emails all users with the Subscriber role a short announcement with the title, excerpt and link whenever a new blog post is published, sent in small batches to protect your mail server.

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

Code

$wpide_announce = array(
    'role'       => 'subscriber',
    'post_type'  => 'post',
    'batch_size' => 50,
);

add_action( 'transition_post_status', function ( $new, $old, $post ) use ( $wpide_announce ) {
    if ( 'publish' !== $new || 'publish' === $old || $wpide_announce['post_type'] !== $post->post_type || get_post_meta( $post->ID, '_wpide_announced', true ) ) {
        return;
    }

    update_post_meta( $post->ID, '_wpide_announced', time() );
    wp_schedule_single_event( time() + MINUTE_IN_SECONDS, 'wpide_announce_batch', array( (int) $post->ID, 0 ) );
}, 10, 3 );

add_action( 'wpide_announce_batch', function ( $post_id = 0, $offset = 0 ) use ( $wpide_announce ) {
    $post = get_post( (int) $post_id );
    if ( ! $post || 'publish' !== $post->post_status ) {
        return;
    }

    $emails = get_users( array( 'role' => $wpide_announce['role'], 'fields' => 'user_email', 'number' => $wpide_announce['batch_size'], 'offset' => (int) $offset, 'orderby' => 'ID' ) );
    if ( ! $emails ) {
        return;
    }

    $site    = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
    $excerpt = wp_trim_words( wp_strip_all_tags( $post->post_excerpt ? $post->post_excerpt : strip_shortcodes( $post->post_content ) ), 50 );
    $headers = array( 'Bcc: ' . implode( ',', array_filter( $emails, 'is_email' ) ) );

    wp_mail( get_option( 'admin_email' ), sprintf( '[%s] New post: %s', $site, wp_strip_all_tags( get_the_title( $post ) ) ), $excerpt . "\n\nRead more: " . get_permalink( $post ) . "\n\nYou receive this because you have an account on $site. Update your preferences: " . admin_url( 'profile.php' ), $headers );

    if ( count( $emails ) === $wpide_announce['batch_size'] ) {
        wp_schedule_single_event( time() + 2 * MINUTE_IN_SECONDS, 'wpide_announce_batch', array( (int) $post_id, (int) $offset + $wpide_announce['batch_size'] ) );
    }
}, 10, 2 );

Notes

Sends 50 BCC recipients per batch every two minutes (change the role, batch size or post type at the top); use a proper newsletter service for large lists.

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.