0

I have a cpt called "auto" and i make a frontend form to create new post with "pending" status. After the post submission i would receive an email with a notification of the new post.

function newpost_notify() {

  $mailadmin = 'xxx@domain.com';
  $subject = 'Subject';
  $headers  = 'MIME-Version: 1.0' . "\r\n";
  $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";    
  $headers .= 'From: xxx <noreply@domain.com>' . "\r\n";
  $message = 'There\'s a new post.';

  wp_mail( $mailadmin, $subject, $message, $headers );
}

add_action( 'publish_post', 'newpost_notify', 10, 2 );

This is my code..but i didn't receive any email.

I would know if there's a difference between a "new post" and a "publish post", because i read something about the post status transition.

Thank you

  • If you read something about post status transition you should know, that you can shoot yourself in the foot very well with what you got there. – flomei Nov 5 '15 at 17:43
  • 1
    @flomei it would be more constructive to explain the issues, rather than just make fun of them. – Rarst Nov 5 '15 at 19:41
  • 1
    You´re right. For clarification: publish_post can really backfire at you, because it runs whenever a post/page/custom post type is saved. So you should not use it for sending mails. Using Post Status Transitions, for example pending_to_publish, would be a better way, cause they are run only when a post/page is "really" published... – flomei Nov 6 '15 at 1:22
  • Thank you for the explanation. Now I think I understand. But I'm not sure is necessary to use a post-status transition...because the post don't come from another status. I wish that the notification was sent when the post is created. There is the status "new" which I think is more appropriate. Correct me if I'm wrong, but you do not think is right "new_auto" {status}_{post_type}? – sb0k Nov 6 '15 at 8:40
  • 1
    You should post your solution as an answer, instead of an edit. It's OK to answer your own question, and you can accept it after 48 hours, so the question will not remain open and unanswered. – Gabriel Nov 6 '15 at 13:51
0

EDIT - SOLUTION

I got the solution using status 'new_to_pending' and adding the $auto object.

function newpost_notify( $auto ) {

  $mailadmin = 'xxx@domain.com';
  $subject = 'Subject';
  $headers  = 'MIME-Version: 1.0' . "\r\n";
  $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";    
  $headers .= 'From: xxx <noreply@domain.com>' . "\r\n";
  $message = 'There\'s a new post.';

  wp_mail( $mailadmin, $subject, $message, $headers );
}

add_action( 'new_to_pending', 'newpost_notify', 10, 2 );

thanks to the constructive comments of @Rarst & @flomei

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.