Send Comment Notification Email To Author

Send Comment Notification Email To Author In WordPress

目錄

Original Posts

Send Comment Notification Email To Author In WordPress


When a user left a comment on your site, and the comment get a new reply, this code will send a notification email to the author. Also, you can send a copy to yourself with Bcc to ensure this feature working fine.

However, there is another issue. With default settings in WordPress, the email filed in comment form is not required. And that’s what we would customize in the next post.

add_action( 'comment_post', 'send_comment_notification_mail', 10, 2 );
function send_comment_notification_mail( $comment_ID, $comment_approved ) {

    if( 1 === $comment_approved ){

        $comment = get_comment( $comment_ID );

        if($comment->comment_parent != 0){

            $parent_id = $comment->comment_parent;
            $parent_comment = get_comment( $parent_id );
            $author_mail = $parent_comment->comment_author_email;
            $post_link = get_permalink($parent_comment->comment_post_ID);
            $post_title = get_the_title($parent_comment->comment_post_ID);

            $comment_link = $post_link."#comment-".$parent_id;
            $comment_link = urldecode($comment_link);


            $to = $parent_comment->comment_author." <".$author_mail.">";
            $content = "Your comment in '".$post_title."' has a new reply。\n".
                                 $comment_link;


            $headers[] = 'From: ARON HACK <aron@aronhack.com>';
            // $headers[] = 'Cc: aronhack2018@gmail.com';
            $headers[] = 'Bcc: aronhack2018@gmail.com';


            wp_mail($to,
                    "[AROH HACK] Comment Reply Notification",
                    $content,
                    $headers);
        }
    }
}

Hooks Used In This Tutorial

get_comments
wp_mail

Related Posts

The Complete WordPress Customization Tutorial – Just Copy And Paste And Work

Content Protection by DMCA.com
A
Aron

Leave a Reply

Your email address will not be published. Required fields are marked *