Title: wp_filter_comment
Published: April 25, 2014
Last modified: February 24, 2026

---

# wp_filter_comment( array $commentdata ): array

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_filter_comment/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/wp_filter_comment/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_filter_comment/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_filter_comment/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/wp_filter_comment/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/wp_filter_comment/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_filter_comment/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_filter_comment/?output_format=md#wp--skip-link--target)

Filters and sanitizes comment data.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_filter_comment/?output_format=md#description)󠁿

Sets the comment data ‘filtered’ field to true when finished. This can be checked
as to whether the comment should be filtered and to keep from filtering the same
comment more than once.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_filter_comment/?output_format=md#parameters)󠁿

 `$commentdata`arrayrequired

Contains information on the comment.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_filter_comment/?output_format=md#return)󠁿

 array Parsed comment information.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_filter_comment/?output_format=md#source)󠁿

    ```php
    function wp_filter_comment( $commentdata ) {
    	if ( isset( $commentdata['user_ID'] ) ) {
    		/**
    		 * Filters the comment author's user ID before it is set.
    		 *
    		 * The first time this filter is evaluated, `user_ID` is checked
    		 * (for back-compat), followed by the standard `user_id` value.
    		 *
    		 * @since 1.5.0
    		 *
    		 * @param int $user_id The comment author's user ID.
    		 */
    		$commentdata['user_id'] = apply_filters( 'pre_user_id', $commentdata['user_ID'] );
    	} elseif ( isset( $commentdata['user_id'] ) ) {
    		/** This filter is documented in wp-includes/comment.php */
    		$commentdata['user_id'] = apply_filters( 'pre_user_id', $commentdata['user_id'] );
    	}

    	/**
    	 * Filters the comment author's browser user agent before it is set.
    	 *
    	 * @since 1.5.0
    	 *
    	 * @param string $comment_agent The comment author's browser user agent.
    	 */
    	$commentdata['comment_agent'] = apply_filters( 'pre_comment_user_agent', ( isset( $commentdata['comment_agent'] ) ? $commentdata['comment_agent'] : '' ) );
    	/** This filter is documented in wp-includes/comment.php */
    	$commentdata['comment_author'] = apply_filters( 'pre_comment_author_name', $commentdata['comment_author'] );
    	/**
    	 * Filters the comment content before it is set.
    	 *
    	 * @since 1.5.0
    	 *
    	 * @param string $comment_content The comment content.
    	 */
    	$commentdata['comment_content'] = apply_filters( 'pre_comment_content', $commentdata['comment_content'] );
    	/**
    	 * Filters the comment author's IP address before it is set.
    	 *
    	 * @since 1.5.0
    	 *
    	 * @param string $comment_author_ip The comment author's IP address.
    	 */
    	$commentdata['comment_author_IP'] = apply_filters( 'pre_comment_user_ip', $commentdata['comment_author_IP'] );
    	/** This filter is documented in wp-includes/comment.php */
    	$commentdata['comment_author_url'] = apply_filters( 'pre_comment_author_url', $commentdata['comment_author_url'] );
    	/** This filter is documented in wp-includes/comment.php */
    	$commentdata['comment_author_email'] = apply_filters( 'pre_comment_author_email', $commentdata['comment_author_email'] );

    	$commentdata['filtered'] = true;

    	return $commentdata;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/comment.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/comment.php#L2190)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/comment.php#L2190-L2242)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/wp_filter_comment/?output_format=md#hooks)󠁿

 [apply_filters( ‘pre_comment_author_email’, string $author_email_cookie )](https://developer.wordpress.org/reference/hooks/pre_comment_author_email/)

Filters the comment author’s email cookie before it is set.

 [apply_filters( ‘pre_comment_author_name’, string $author_cookie )](https://developer.wordpress.org/reference/hooks/pre_comment_author_name/)

Filters the comment author’s name cookie before it is set.

 [apply_filters( ‘pre_comment_author_url’, string $author_url_cookie )](https://developer.wordpress.org/reference/hooks/pre_comment_author_url/)

Filters the comment author’s URL cookie before it is set.

 [apply_filters( ‘pre_comment_content’, string $comment_content )](https://developer.wordpress.org/reference/hooks/pre_comment_content/)

Filters the comment content before it is set.

 [apply_filters( ‘pre_comment_user_agent’, string $comment_agent )](https://developer.wordpress.org/reference/hooks/pre_comment_user_agent/)

Filters the comment author’s browser user agent before it is set.

 [apply_filters( ‘pre_comment_user_ip’, string $comment_author_ip )](https://developer.wordpress.org/reference/hooks/pre_comment_user_ip/)

Filters the comment author’s IP address before it is set.

 [apply_filters( ‘pre_user_id’, int $user_id )](https://developer.wordpress.org/reference/hooks/pre_user_id/)

Filters the comment author’s user ID before it is set.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_filter_comment/?output_format=md#related)󠁿

| Uses | Description | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

| Used by | Description | 
| [WP_REST_Comments_Controller::create_item()](https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/create_item/)`wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php` |

Creates a comment.

  | 
| [wp_new_comment()](https://developer.wordpress.org/reference/functions/wp_new_comment/)`wp-includes/comment.php` |

Adds a new comment to the database.

  | 
| [wp_update_comment()](https://developer.wordpress.org/reference/functions/wp_update_comment/)`wp-includes/comment.php` |

Updates an existing comment in the database.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_filter_comment/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.0.0](https://developer.wordpress.org/reference/since/2.0.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_filter_comment%2F)
before being able to contribute a note or feedback.