最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Send a mail to specific address in a custom field when a new comment is made on a specific post

programmeradmin2浏览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 );

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
Add a comment  | 

1 Answer 1

Reset to default 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/reference/hooks/comment_old_status_to_new_status/

发布评论

评论列表(0)

  1. 暂无评论