0

I am trying to send a mail using wp_mail function when a new comment is made to a specific post where a custom field is compiled with a mail.

The function works, but when i try to restrict it to unapproved or approved comments (not for spam) it won't work. I have a function that send every new comment to spam, and after a moderator will approve the comment or move it out from spam folder (so the status will change to approved or not approved) i would like to send a mail to a specific address:

// SEND NEW COMMENTS TO SPAM
function set_new_comment_to_spam($commentId) {
    wp_set_comment_status($commentId, 'spam');
}

add_action('comment_post', 'set_new_comment_to_spam', 10, 1);
// SEND MAIL TO CUSTOM FIELD
function send_comment_email_notification( $comment_ID, $comment_approved, $commentdata) {
    $comment = get_comment( $comment_ID );
    $postid = $comment->comment_post_ID;
    $master_email = get_post_meta( $postid, 'email_custom', true);
    if( $comment_approved != 'spam' && isset( $master_email ) && is_email( $master_email ) ) {
        $message = 'New comment';
        add_filter( 'wp_mail_content_type', create_function( '', 'return "text/html";' ) );
        wp_mail( $master_email, 'New comment', $message );
    }
}
add_action( 'comment_post', 'send_comment_email_notification', 11, 3 );

1 Answer 1

1

It sounds like it's not working because you're hooking into comment_post, which is when the comment is created If you change the status of the comment, the comment already exists, and therefore no email is generated.

There is a comment status change hook: comment_{$old_status}_to_{$new_status}.

It fires anytime a comment's status changes from one specified status to another.

So you might try something like this instead:

function send_comment_email_notification($comment) {
    $master_email = get_post_meta( $comment->comment_post_ID, 'email_custom', true);
    if( isset( $master_email ) && is_email( $master_email ) ) {
        $message = 'New comment';
        add_filter( 'wp_mail_content_type', create_function( '', 'return "text/html";' ) );
        wp_mail( $master_email, 'New comment', $comment->comment_content );
    }
}
add_action('comment_spam_to_approved', 'send_comment_email_notification');

Documentation here: https://developer.wordpress.org/reference/hooks/comment_old_status_to_new_status/

3
  • will this send a mail when the comment is changed to approved or not approved only? Shouldn't be an if statement like: if( $status = 'approved' || $status = 'not approved' where $status is equal to wp_get_comment_status?
    – Benjamin
    Aug 20, 2020 at 16:32
  • @Benjamin It will only send an email if the comment is changed from spam to approved, because that's the only time the hook is fired. Also, you may consider storing a comment meta that an email was sent, so that when status is changed back to spam and back to approved, not another email is fired off.
    – NightHawk
    Aug 20, 2020 at 20:24
  • i've tried with your function but it's not working unluckly :( will check it out later to see what could be wrong with it
    – Benjamin
    Aug 25, 2020 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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