Filter Hook since 0.71

comment_text

Filters the displayed text of a single comment

Description

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.

When it runs

Each time comment_text() runs — usually inside wp_list_comments()'s callback.

Signature

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

Parameters

  • $comment_text string — Comment content.
  • $comment WP_Comment|null — The comment object.
  • $args array — Arguments passed to comment_text().

Examples

Basic

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 );

Append a 'Guest' badge to comments from non-registered users.

Common Use Cases

  • Add badges/labels to comment output
  • Auto-link mentions or hashtags in comments
  • Strip disallowed markup on display

Common mistakes

  • Editing comment content here to 'fix' spam — this only changes display, not the stored comment; moderation belongs in preprocess_comment or comment status hooks

Related hooks

FAQ

Does filtering comment_text change the comment stored in the database?

No — it only changes what's rendered on display. The database row is untouched.

Source: wp-includes/comment-template.php

Group wizard

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

comment_text → the_content

functions.php — comment_text
functions.php
PHP comment_text · Hook 1 of 2 · Step 1 of 6

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