Send specific users an email when posts are published

I’ve got the following code, which is supposed to run when a new post is published for the first time, check if a custom field ‘specific_users’ has data (this uses Advanced Custom Fields), and if so, send an email to each of the selected users.

/**
 * Send users a notification of new grower posts 
 */
function notify_growers($post) {
// Only notify for grower posts
if ( $post->post_type != 'grower_posts' ) return;

$post_id = $post->ID;

// ...run code once
if ( !get_post_meta( $post_id, 'emailsent', $single = true ) ) {

    $specific_users = get_field('specific_users',$post_id);
    if(isset($specific_users) && is_array($specific_users)) {
        foreach($specific_users as $specific_user) {

            $to = $specific_user['user_email'];
            $subject="Grower News: " . get_the_title($post_id);

            $message="";
            $message .= '<p>' . get_the_excerpt($post_id) . '…</p>';
            $message .= '<p><a href="' . get_permalink($post_id) . '"><strong>Continue Reading &raquo;</strong></a></p>';

            wp_mail($to, $subject, $message );

        }
        unset($specific_user);
        //print("<pre>User IDs = ".print_r($user_ids,true)."</pre>");
    } else { return; }

    // Make sure this doesn't run again
    update_post_meta( $post_id, 'emailsent', true );
}

}
add_action( 'draft_to_publish', 'notify_growers' );
add_action( 'new_to_publish', 'notify_growers' );

It’s not sending any emails, and I’m not sure how to go about debugging it.

Side question, is sending the email in the foreach loop the best way to do it? Or should I do it all at once with BCCs somehow?

Thanks,
Angus

2 Answers
2

Do you know if your wp_mail() function is working at all?
Do you know whether or not your function isn’t failing higher in the chain?

I’d re-write the function like this, which makes it a little easier to read and also slightly more efficient as we are only running functions that we absolutely need to given a certain condition being met.

Also, I’ve removed the get_field() function call which is just a wrapper for get_post_meta().

Storing $post_id = $post->ID has also been removed because at this stage there is no need to manipulate or cache the ID for any particular reason, so it just cleans things up a little.

If you know that your call to the specific_users custom field returns a serialized array then you can add the true parameter after the meta_key to receive back an unserialized array, otherwise omit the true value.

isset will return true even if the array is empty and IF there are no results in the array WordPress will in fact return an empty array, unless the true parameter is set in which case it will return an empty string, so a call the call to isset has been removed and replaced with !empty($array).

From this point you can debug the function somewhat easier…

function notify_growers($post) {
    //if NOT post type 'grower_posts' then exit;
    if ( $post->post_type != 'grower_posts' ) 
         return;

    //if email already sent then exit.
    if ( get_post_meta( $post->ID, 'emailsent', true ) )
         return;

    //if email not sent then get an array of users to email
    $specific_users = get_post_meta($post->ID, 'specific_users');

    //if $specific_users is an array and is not empty then send email. 
    if(is_array($specific_users) && !empty($specific_users)) { 
        foreach($specific_users as $specific_user) {
            //add mail logic here, $to, $subject, $message
            wp_mail($to, $subject, $message );
        }
        unset($specific_user);

        //prevent emails from being sent by updating post meta field
        update_post_meta( $post->ID, 'emailsent', true );
    }   

}

add_action( 'draft_to_publish', 'notify_growers' );
add_action( 'new_to_publish', 'notify_growers' );

Leave a Comment