0

I can't seem to figure out why untrashing a comment, calls my "approve_comment_callback"-action.

Any ideas? I just don't want it to send two emails at the same time, when I untrash a comment.

Here are the two (mail)functions that seem to collide...

    // Send mail to user when their comment has been restored
function untrash_answer_notification($comment_id) {
$comment = get_comment($comment_id);
if ( $comment->comment_parent != 0 ) return;
$author = get_userdata($comment->user_id);
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
$headers .= "From: Blabla.com <bla@bla.com>" . "\r\n";
$subject = "Your comment was restored";
$message = "blabla";

wp_mail($author->user_email, $subject, $message, $headers);
}
add_action( 'untrash_comment', 'untrash_answer_notification' );

// Mail author if/when they're comment is approved
function approve_comment_callback($new_status, $old_status, $comment) {
if($old_status != $new_status) {
if($new_status == 'approved' || $new_status==1) {
if($comment->comment_author_email!=''){
$author = get_userdata($comment->user_id);
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
$headers .= "From: Blabla.com <bla@bla.com>" . "\r\n";
$subject = "Your comment was approved";
$message = "blabla";

wp_mail($author->user_email, $subject, $message, $headers);
    }
    }
        }
}
add_action('transition_comment_status', 'approve_comment_callback', 10,3);
3
  • 1
    your question title seems to be stating the inverse of your question text. are you approving or un-trashing? – Milo Mar 8 '14 at 19:07
  • You seem to have neglected closing brackets - in it's present form, your code should do nothing but throw errors. – bosco Mar 8 '14 at 20:40
  • I've added the brackets. Just a simple mail function. – dkl Mar 9 '14 at 9:57
0

The transition_comment_status action will get called whenever the status of a comment changes from one thing to another thing, regardless of what those things are.

If you don't want it to take action when going from trashed to approved, you need to recognize when the $old_status is trashed and not take action at that point in time. Right now, you're only checking that the $new_status is approved, you're not checking what the old status was.

1
  • The $old_status should be "pending" in that case... right? – dkl Mar 11 '14 at 8:41

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.