0

I am working on some automated emails. I am fairly new to add_action and do_action. I am having a tough time understanding it. The site in question has number goals set up that people reach over time. I want to send them an email when they hit the goal. The code below will send that email when the post is updated but that won't work. I put it there just to make sure the email sends out. I don't know what add_action I should use. What reference should I be looking for to do this?

function emailNotificationSuccess($post_id) {

  if( ( $_POST['post_status'] == 'publish' ) && ( $goalsupport === $totalsupport ) ) {

   $post = get_post($post_id);
   $author = get_userdata($post->post_author);
   $totalsupport = countSupportForAll($post->ID);
   $goalsupport = get_post_meta( $post->ID, 'sj_campaign_goal', true );
   $supporter_email = get_post_meta( $post_id, 'sj_campaign_email', true );
   $message = "Hi ".$author->display_name.",


Congrats! You have reached your goal!

Your , ".$post->post_title." is a success!

".get_permalink( $post_id )."


   ";
   wp_mail($supporter_email, "You just reached your goal!", $message);

  } 
} 
add_action('post_updated', 'emailNotificationSuccess');
0

Your problem is with the conditional, not with the used action (I think).

This line is probably not verifying in any situation:

if( ( $_POST['post_status'] == 'publish' ) && ( $goalsupport === $totalsupport ) ) {

I can not now what $goalsupport and $goalsupport are, both variables are undefined in your code, so I'm going to remove them. Change that line with this one:

if( get_post_status( $post_id ) == 'publish' ) {

So, the code should be:

function emailNotificationSuccess($post_id) {

  if( get_post_status( $post_id ) == 'publish' ) {

   $post = get_post($post_id);
   $author = get_userdata($post->post_author);
   $totalsupport = countSupportForAll($post->ID);
   $goalsupport = get_post_meta( $post->ID, 'sj_campaign_goal', true );
   $supporter_email = get_post_meta( $post_id, 'sj_campaign_email', true );
   $message = "Hi ".$author->display_name.",


Congrats! You have reached your goal!

Your , ".$post->post_title." is a success!

".get_permalink( $post_id )."


   ";
   wp_mail($supporter_email, "You just reached your goal!", $message);

  } 
} 
add_action('post_updated', 'emailNotificationSuccess');
  • If I take those out then there is no way to tell when it has hit its goal. – JediTricks007 Apr 28 '15 at 2:20
  • As I said in the answer, $goalsupport and $totalsupport are undefined variables in your code, so you can not check them; that is why I removed them, there is nothing to check anyway. Try to publish a post with my code and you will see. Then try to build your "goal" checking. – cybmeta Apr 28 '15 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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