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

  • $id int — The new comment's ID.
  • $comment WP_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

Group wizard

One session: this hook, then each related_hooks entry. Walk it and tell us what to change.

wp_insert_comment → comment_post

functions.php — wp_insert_comment
functions.php
PHP wp_insert_comment · Hook 1 of 2 · Step 1 of 7

Practice the wp_insert_comment WordPress action hook in our interactive sandbox — a VS Code-style editor with a step-by-step wp_insert_comment tutorial, add_action exercises, and instant feedback. No local WordPress install required.