wppaste
WordPress

wp_allow_comment( array $commentdata, bool $wp_error = false ): int|string|WP_Error

Since
2.0.0, 4.7.0, 5.5.0
Source
wp-includes/comment.php:685
Validates whether this comment is allowed to be made.

Compatibility

WordPress
since 5.5.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.
$wp_errorbooloptional
When true, a disallowed comment will result in the function returning a WP_Error object, rather than executing wp_die().
Default false.Default: false

Return value

int|string|WP_Error
Allowed comments return the approval status (0|1|'spam'|'trash').
If $wp_error is true, disallowed comments return a WP_Error.

Performance profile

How much work a call to wp_allow_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
Heavy

Reaches the database via ->get_var().

Scaling
Constant

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

Instructions
71–128

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

Plugin surface
6 hooks

Third-party callbacks on 'duplicate_comment_id', 'comment_duplicate_trigger', 'comment_duplicate_message' run inside this call, and their cost is not bounded by anything here.

Called by
2

2 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
  • sqldatabase query->get_var()called directly

Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 14 distinct outcomes

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

WhenInstructionsCalls it makes
!$commentdata71wp_unslash(), wp_unslash(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), ->get_var(), apply_filters(), do_action(), __(), apply_filters()
$commentdata81wp_unslash(), wp_unslash(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), ->get_var(), apply_filters(), do_action(), __(), apply_filters()
!$commentdata85wp_unslash(), wp_unslash(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), ->get_var(), apply_filters(), do_action(), apply_filters(), wp_check_comment_data()
!$commentdata95wp_unslash(), wp_unslash(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), ->get_var(), apply_filters(), do_action(), apply_filters(), __(), apply_filters()
$commentdata95wp_unslash(), wp_unslash(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), ->get_var(), apply_filters(), do_action(), apply_filters(), wp_check_comment_data()
$commentdata105wp_unslash(), wp_unslash(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), ->get_var(), apply_filters(), do_action(), apply_filters(), __(), apply_filters()
!$commentdata && !wp_doing_ajax()105wp_unslash(), wp_unslash(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), ->get_var(), apply_filters(), do_action(), __(), apply_filters(), wp_doing_ajax(), wp_die(), do_action(), apply_filters(), wp_check_comment_data()
!$commentdata && wp_doing_ajax()108wp_unslash(), wp_unslash(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), ->get_var(), apply_filters(), do_action(), __(), apply_filters(), wp_doing_ajax(), exit(), wp_die(), do_action(), apply_filters(), wp_check_comment_data()
!$commentdata && !wp_doing_ajax()115wp_unslash(), wp_unslash(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), ->get_var(), apply_filters(), do_action(), __(), apply_filters(), wp_doing_ajax(), wp_die(), do_action(), apply_filters(), __(), apply_filters()
$commentdata && !wp_doing_ajax()115wp_unslash(), wp_unslash(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), ->get_var(), apply_filters(), do_action(), __(), apply_filters(), wp_doing_ajax(), wp_die(), do_action(), apply_filters(), wp_check_comment_data()
!$commentdata && wp_doing_ajax()118wp_unslash(), wp_unslash(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), ->get_var(), apply_filters(), do_action(), __(), apply_filters(), wp_doing_ajax(), exit(), wp_die(), do_action(), apply_filters(), __(), apply_filters()
$commentdata && wp_doing_ajax()118wp_unslash(), wp_unslash(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), ->get_var(), apply_filters(), do_action(), __(), apply_filters(), wp_doing_ajax(), exit(), wp_die(), do_action(), apply_filters(), wp_check_comment_data()
2 further outcomes, up to 128 instructions
$commentdata && !wp_doing_ajax()125wp_unslash(), wp_unslash(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), ->get_var(), apply_filters(), do_action(), __(), apply_filters(), wp_doing_ajax(), wp_die(), do_action(), apply_filters(), __(), apply_filters()
$commentdata && wp_doing_ajax()128wp_unslash(), wp_unslash(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), ->get_var(), apply_filters(), do_action(), __(), apply_filters(), wp_doing_ajax(), exit(), wp_die(), do_action(), apply_filters(), __(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev13871–1285
8.513871–1285
8.413871–12852 more instructions than PHP 8.3
8.313669–1255
8.213669–1255
8.113669–1255
7.413669–1255

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 · 6

6 hooks fire while wp_allow_comment() runs, in this order:

  1. apply_filters( duplicate_comment_id )filterline 721 (+36 into the body)

    Filters the ID, if any, of the duplicate comment found when creating a new comment.

  2. do_action( comment_duplicate_trigger )actionline 731 (+46 into the body)

    Fires immediately after a duplicate comment is detected.

  3. apply_filters( comment_duplicate_message )filterline 740 (+55 into the body)

    Filters duplicate comment error message.

  4. do_action( check_comment_flood )actionline 768 (+83 into the body)

    Fires immediately before a comment is marked approved.

  5. apply_filters( wp_is_comment_flood )filterline 791 (+106 into the body)

    Filters whether a comment is part of a comment flood.

  6. apply_filters( comment_flood_message )filterline 802 (+117 into the body)

    Filters the comment flood error message.

Uses · 8

  • wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • do_action()Calls the callback functions that have been added to an action hook.
  • __()Retrieves the translation of $text.
  • wp_doing_ajax()Determines whether the current request is a WordPress Ajax request.
  • wp_die()Kills WordPress execution and displays HTML page with an error message.
  • wp_check_comment_data()Checks whether comment data passes internal checks or has disallowed content.
  • WP_Error::__construct()Initializes the error.

Used by · 2

Source code

function wp_allow_comment( $commentdata, $wp_error = false ) {	global $wpdb; 	/*	 * Simple duplicate check.	 * expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)	 */	$dupe = $wpdb->prepare(		"SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = %s AND comment_approved != 'trash' AND ( comment_author = %s ",		wp_unslash( $commentdata['comment_post_ID'] ),		wp_unslash( $commentdata['comment_parent'] ),		wp_unslash( $commentdata['comment_author'] )	);	if ( $commentdata['comment_author_email'] ) {		$dupe .= $wpdb->prepare(			'AND comment_author_email = %s ',			wp_unslash( $commentdata['comment_author_email'] )		);	}	$dupe .= $wpdb->prepare(		') AND comment_content = %s LIMIT 1',		wp_unslash( $commentdata['comment_content'] )	); 	$dupe_id = $wpdb->get_var( $dupe ); 	/**	 * Filters the ID, if any, of the duplicate comment found when creating a new comment.	 *	 * Return an empty value from this filter to allow what WP considers a duplicate comment.	 *	 * @since 4.4.0	 *	 * @param int   $dupe_id     ID of the comment identified as a duplicate.	 * @param array $commentdata Data for the comment being created.	 */	$dupe_id = apply_filters( 'duplicate_comment_id', $dupe_id, $commentdata ); 	if ( $dupe_id ) {		/**		 * Fires immediately after a duplicate comment is detected.		 *		 * @since 3.0.0		 *		 * @param array $commentdata Comment data.		 */		do_action( 'comment_duplicate_trigger', $commentdata ); 		/**		 * Filters duplicate comment error message.		 *		 * @since 5.2.0		 *		 * @param string $comment_duplicate_message Duplicate comment error message.		 */		$comment_duplicate_message = apply_filters( 'comment_duplicate_message', __( 'Duplicate comment detected; it looks as though you’ve already said that!' ) ); 		if ( $wp_error ) {			return new WP_Error( 'comment_duplicate', $comment_duplicate_message, 409 );		} else {			if ( wp_doing_ajax() ) {				die( $comment_duplicate_message );			} 			wp_die( $comment_duplicate_message, 409 );		}	} 	/**	 * Fires immediately before a comment is marked approved.	 *	 * Allows checking for comment flooding.	 *	 * @since 2.3.0	 * @since 4.7.0 The `$avoid_die` parameter was added.	 * @since 5.5.0 The `$avoid_die` parameter was renamed to `$wp_error`.	 *	 * @param string $comment_author_ip    Comment author's IP address.	 * @param string $comment_author_email Comment author's email.	 * @param string $comment_date_gmt     GMT date the comment was posted.

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.

5.5.0
The $avoid_die parameter was renamed to $wp_error.from the docblock
4.7.0
The $avoid_die parameter was added, allowing the function to return a WP_Error object instead of dying.from the docblock
2.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 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.