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 );