The comment_post WordPress PHP action fires immediately after a comment is inserted into the database.
Usage
add_action('comment_post', 'your_custom_function', 10, 3);
function your_custom_function($comment_id, $comment_approved, $commentdata) {
// your custom code here
}
Parameters
$comment_id(int) – The comment ID.$comment_approved(int|string) – 1 if the comment is approved, 0 if not, ‘spam’ if spam.$commentdata(array) – Comment data.
More information
See WordPress Developer Resources: comment_post
Examples
Send a notification email when a new comment is posted
add_action('comment_post', 'send_notification_email', 10, 3);
function send_notification_email($comment_id, $comment_approved, $commentdata) {
// Get the post author email
$post_author_email = get_the_author_meta('user_email', $commentdata['user_ID']);
// Send the email
wp_mail($post_author_email, 'New Comment on Your Post', 'A new comment has been posted on your post.');
}