wppaste
WordPress

wp_filter_comment( array $commentdata ): array

Since
2.0.0
Source
wp-includes/comment.php:2251
Filters and sanitizes comment data.

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.

Compatibility

WordPress
since 2.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$commentdataarray
Contains information on the comment.

Return value

array
Parsed comment information.

Performance profile

How much work a call to wp_filter_comment() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
56–65

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 74.

Plugin surface
7 hooks

Third-party callbacks on 'pre_user_id', 'pre_comment_user_agent', 'pre_comment_author_name' run inside this call, and their cost is not bounded by anything here.

Called by
3

3 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly

What one call costs · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_filter_comment() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!isset($commentdata)56–57apply_filters(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), apply_filters()
isset($commentdata)63–65apply_filters(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), apply_filters()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 74 instructions, 56–65 executed per call, 3 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 8

8 hooks fire while wp_filter_comment() runs, in this order:

  1. apply_filters( pre_user_id )filterline 2263 (+12 into the body)

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

  2. apply_filters( pre_user_id )filterline 2266 (+15 into the body)

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

  3. apply_filters( pre_comment_user_agent )filterline 2276 (+25 into the body)

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

  4. apply_filters( pre_comment_author_name )filterline 2278 (+27 into the body)

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

  5. apply_filters( pre_comment_content )filterline 2286 (+35 into the body)

    Filters the comment content before it is set.

  6. apply_filters( pre_comment_user_ip )filterline 2294 (+43 into the body)

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

  7. apply_filters( pre_comment_author_url )filterline 2296 (+45 into the body)

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

  8. apply_filters( pre_comment_author_email )filterline 2298 (+47 into the body)

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

Uses · 1

  • apply_filters()Calls the callback functions that have been added to a filter hook.

Used by · 3

Source code

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', ( $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;}

Changelog

Introduced in 2.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/comment.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.