Action Hook since 2.8.0
wp_insert_comment
Fires right after a comment row is written to the database
Description
wp_insert_comment is the lower-level sibling of comment_post — it fires for every comment insert, including ones triggered programmatically, not just front-end submissions.
When it runs
Runs inside wp_insert_comment(), called by wp_new_comment() (front-end) as well as directly by REST API and import code.
Signature
do_action( 'wp_insert_comment', int $id, WP_Comment $comment );Parameters
$idint — The new comment's ID.$commentWP_Comment — The full comment object.
Examples
Basic
add_action( 'wp_insert_comment', function( $id, $comment ) {
error_log( 'New comment #' . $id . ' on post ' . $comment->comment_post_ID );
}, 10, 2 );Log every comment insert regardless of where it came from.
Common Use Cases
- React to comments created via REST API or import scripts, not just the comment form
- Sync a WP_Comment object right after creation
- Trigger downstream logic that needs the full comment object rather than raw array data
Common mistakes
- Using this hook to gate front-end-only logic — it also fires for programmatic/REST inserts where typical request context (like $_POST) doesn't exist
Related hooks
FAQ
wp_insert_comment vs comment_post — which should I use?
comment_post is specific to the standard front-end submission flow and gives you the approval status directly. wp_insert_comment fires for every insert path (including REST/import) and gives you the full WP_Comment object.
Source: wp-includes/comment.php