comment_post
Fires immediately after a comment is added to the database. The event passes the comment ID, status, and data.
For authors who have permission to edit comments, a notification about comments on their posts is sent automatically when the following option is enabled: Settings > Discussion > Comment author must have a previously approved comment. The wp_notify_moderator() function sends these emails.
Usage
add_action( 'comment_post', 'wp_kama_comment_post_action', 10, 3 );
/**
* Function for `comment_post` action-hook.
*
* @param int $comment_id The comment ID.
* @param int|string $comment_approved 1 if the comment is approved, 0 if not, 'spam' if spam.
* @param array $commentdata Comment data.
*
* @return void
*/
function wp_kama_comment_post_action( $comment_id, $comment_approved, $commentdata ){
// action...
}
Parameters
- $comment_ID(int)
- ID of the comment added to the database.
- $comment_approved(string/int)
Comment approval status. Possible values:
0(false) — not approved.1(true) — approved.'spam'— spam.
- $commentdata(array) (WP 4.5)
Comment data as an associative array. Array keys correspond to database table fields. Example:
$data = array( 'comment_post_ID' => 1, 'comment_author' => 'admin', 'comment_author_email' => '[email protected]', 'comment_author_url' => 'http://', 'comment_content' => 'Comment text', 'comment_type' => '', 'comment_parent' => 0, 'user_id' => 1, 'comment_author_IP' => '127.0.0.1', 'comment_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)', 'comment_date' => current_time('mysql'), 'comment_approved' => 1, );
Examples
#1 Notify the post author about a comment on their post
Suppose several authors publish posts on the site, and an email notification should be sent to the author whenever a new comment is added to one of their posts:
add_action( 'comment_post', 'author_new_comment_notify', 10, 2 );
function author_new_comment_notify( $comment_ID, $comment_approved ){
// Return if the comment is not approved.
//if( $comment_approved == 0 )
// return;
$comment = get_comment( $comment_ID );
$post = get_post( $comment->comment_post_ID );
$user = get_userdata( $post->post_author );
if( empty( $user->user_email ) )
return;
// Message.
$message = 'New reply to your post: '. $post->post_title . "\r\n";
$message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
$message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
$message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
$message .= 'Reply text:' . "\r\n" . $comment->comment_content . "\r\n\r\n";
// Subject.
$subject = '['.$_SERVER['HTTP_HOST'].'] New reply to your post.';
// Headers.
$headers = 'From: No Answer <noanswer@'. $_SERVER['HTTP_HOST'] .'>' . "\r\n";
@wp_mail( $user->user_email, $subject, $message, $headers );
}
Changelog
| Since 1.2.0 | Introduced. |
| Since 4.5.0 | The $commentdata parameter was added. |
Where the hook is called
comment_post
wp-includes/comment.php 2491
do_action( 'comment_post', $comment_id, $commentdata['comment_approved'], $commentdata );
Where the hook is used in WordPress
wp-includes/default-filters.php 536
add_action( 'comment_post', 'wp_new_comment_notify_moderator' );
wp-includes/default-filters.php 537
add_action( 'comment_post', 'wp_new_comment_notify_postauthor' );