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 );
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 );
Share
Improve this question
asked Aug 20, 2020 at 15:48
BenjaminBenjamin
1656 bronze badges
1 Answer
Reset to default 1It 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/reference/hooks/comment_old_status_to_new_status/