Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
for debuging first see print_r($specific_users) –  Dk-Macadamia Jan 8 at 6:59

3 Answers 3

You can also debugging inside your function. Write the data of vars in a transient with WordPress default functions, like set_transient() or debug in the console of your browser. You can use the plugin Debug Objects for write from php to the console of Webinspector in Chrome.

Maybe it is a good idea to add a exit(); before close brackets od the function to create a output via php possibilities, like var_dump() and stop the function to read easier on screen.

You find more hints on the source in this plugin file, use the wp_mail() function also for the job to inform about posts. Maybe it is a small help to see, how is possible.

share|improve this answer

More inf: http://www.wprecipes.com/how-to-automatically-notify-your-members-on-new-posts

function notify_growers($post_ID)  {
    global $wpdb;

    // 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);
    } else { return; }

    update_post_meta( $post_id, 'emailsent', true );
    return $post_ID;
}

add_action('publish_post', 'notify_growers');
share|improve this answer

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' );
share|improve this answer
    
Thanks, I'll give it a try :) It looks way cleaner than mine. –  Angus Russell May 22 '13 at 8:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.