wppaste
WordPress

wp_filter_comment( array $commentdata ): array

Since
2.0.0
Source
wp-includes/comment.php:2108
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
57–66

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

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)57–58apply_filters(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), apply_filters()
isset($commentdata)64–66apply_filters(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), apply_filters(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev7657–663
8.57657–663
8.47657–663
8.37657–663
8.27657–663
8.17657–6631 fewer instruction than PHP 7.4
7.47757–673

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 2120 (+12 into the body)

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

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

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

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

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

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

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

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

    Filters the comment content before it is set.

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

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

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

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

  8. apply_filters( pre_comment_author_email )filterline 2155 (+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', ( 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;}

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 6.7.7 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.