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.

This question already has an answer here:

I am using a custom function with 'publish_post' hook for a notification to the author when the post has been published but the issue I am facing right now is that the notification is being sent out twice and as well when the post is updated. Here is how my function looks.

function authorNotification($post_id) {
   $post = get_post($post_id);
   $author = get_userdata($post->post_author);

   $message = "
      Hi ".$author->display_name.",
      New post, ".$post->post_title." has just been published at ".get_permalink( $post_id ).".
   ";
   wp_mail($author->user_email, "New Post Published", $message);
}
add_action('publish_post', 'authorNotification');

am I missing something here?

share|improve this question
    
See also this code example. –  toscho Oct 3 '13 at 10:38
add comment

marked as duplicate by toscho Oct 3 '13 at 10:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 0 down vote accepted

You need to write your hook for transition_post_status action:

function authorNotification( $new_status, $old_status, $post ) {
    if ( $new_status == 'publish' && $old_status != 'publish' ) {
        $author = get_userdata($post->post_author);
        $message = "
            Hi ".$author->display_name.",
            New post, ".$post->post_title." has just been published at ".get_permalink( $post->ID ).".
        ";
        wp_mail($author->user_email, "New Post Published", $message);
    }
}
add_action('transition_post_status', 'authorNotification', 10, 3 );
share|improve this answer
    
that did a trick for duplicate notification but I am still receiving a notification when the post is updated. Any thoughts on that? Thanks –  Kevin S Oct 3 '13 at 10:10
    
I have updated the snippet and it will work –  Eugene Manuilov Oct 3 '13 at 10:22
    
That work. I am very new to WordPress so thanks a lot. –  Kevin S Oct 3 '13 at 10:28
add comment

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