Comments

From the moment a visitor submits a comment to the moment it renders on the page, WordPress fires a specific sequence of hooks. This group covers that lifecycle — validation, storage, and display. 2 actions, 1 filters in this category.

comment_text

comments

Filters the displayed text of a single comment

comment_text is where you'd add nofollow to links, auto-linkify text, or moderate content on display — it doesn't change what's stored in the database.

Signature

apply_filters( 'comment_text', string $comment_text, WP_Comment|null $comment, array $args );

Example

add_filter( 'comment_text', function( $comment_text, $comment ) {
    if ( $comment && ! $comment->user_id ) {
        $comment_text .= '<span class="guest-badge">Guest</span>';
    }
    return $comment_text;
}, 10, 2 );

Common Use Cases

  • Add badges/labels to comment output
  • Auto-link mentions or hashtags in comments
  • Strip disallowed markup on display
since 0.71wp-includes/comment-template.php

comment_post

comments

Fires immediately after a comment is inserted

comment_post fires right after a new comment saves — the standard place to trigger notifications or moderation logic.

Signature

do_action( 'comment_post', int $comment_id, int|string $comment_approved, array $comment_data );

Example

add_action( 'comment_post', function( $comment_id, $approved ) {
    if ( 1 === $approved ) {
        wp_mail( get_option( 'admin_email' ), 'New comment', 'A comment was just approved.' );
    }
}, 10, 2 );

Common Use Cases

  • Send custom notification emails
  • Auto-flag suspicious comments for review
  • Sync comments to an external system
  • Award points/badges for commenting
since 1.2.0wp-includes/comment.php

wp_insert_comment

comments

Fires right after a comment row is written to the database

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.

Signature

do_action( 'wp_insert_comment', int $id, WP_Comment $comment );

Example

add_action( 'wp_insert_comment', function( $id, $comment ) {
    error_log( 'New comment #' . $id . ' on post ' . $comment->comment_post_ID );
}, 10, 2 );

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
since 2.8.0wp-includes/comment.php