apply_filters( 'comment_text', string $comment_text , WP_Comment|null $comment , array $args )
Filters the text of a comment to be displayed.
Description Description
See also See also
Parameters Parameters
- $comment_text
-
(string) Text of the current comment.
- $comment
-
(WP_Comment|null) The comment object.
- $args
-
(array) An array of arguments.
Source Source
Changelog Changelog
| Version | Description |
|---|---|
| 1.2.0 | Introduced. |
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
#24913 (comment_text filter used differently in two places in core) – WordPress Trac
In the first (
/wp-includes/comment.php), the$commentvariable contains just the comment text. In the second (/wp-includes/comment-template.php), the$commentvariable contains the actual comment object itself (based on the return ofget_comment( $comment_ID )).So the filters are filtering the same fields. The issue then becomes the number of parameters and that, too, is not a bug.
The first comment text filter is used specifically to filter the text of the comment before things like inserting it into the database. The second is used when filtering the text of the comment before displaying it on a page – this follows the pattern of not trusting data before inserting it in the DB and also not trusting the data when pulling it back out. Using the same filter here helps us use exactly the same rules to filter the content both pre and post DB.
If the problem is the number of fields, simply make the second parameter of your function optional:
function filter_text( $comment_text, $comment = null ) { // Do something to the comment, possibly switching based on the presence of $comment } add_filter( 'comment_text', 'filter_text', 10, 2 );If you only want to do things when the comment is inserted into the DB, just verify that
null === $commentin the function above. If you only want to do things when comments are displayed, verify thatnull !== $comment.IMO this isn’t a bug but a pretty powerful differentiating factor between a filter that can be used in different places to do either the same or different things.
Remove rel=”nofollow” from comment content text
add_filter( 'comment_text', 'remove_rel_nofollow_attribute' ); function remove_rel_nofollow_attribute( $comment_text ) { $comment_text = str_ireplace(' rel="nofollow"', '', $comment_text ); return $comment_text; }Apparently this filter is not called in the “recent comments” widget of the activity (in the dashboard). Too bad (I’m trying to use this filter to obfuscate the content of selected comments, in case the user don’t have the rights to see the commented post).