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:752
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_erroris 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
- Scaling
- Constant
- Instructions
- 71–128
- Plugin surface
- 6 hooks
- Called by
- 2
Reaches the database via ->get_var().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 138.
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.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
!$commentdata | 71 | wp_unslash(), wp_unslash(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), ->get_var(), apply_filters(), do_action(), __(), apply_filters() |
$commentdata | 81 | wp_unslash(), wp_unslash(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), ->get_var(), apply_filters(), do_action(), __(), apply_filters() |
!$commentdata | 85 | wp_unslash(), wp_unslash(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), ->get_var(), apply_filters(), do_action(), apply_filters(), wp_check_comment_data() |
!$commentdata | 95 | wp_unslash(), wp_unslash(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), ->get_var(), apply_filters(), do_action(), apply_filters(), __(), apply_filters() |
$commentdata | 95 | wp_unslash(), wp_unslash(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), wp_unslash(), ->prepare(), ->get_var(), apply_filters(), do_action(), apply_filters(), wp_check_comment_data() |
$commentdata | 105 | wp_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() | 105 | wp_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() | 108 | wp_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() | 115 | wp_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() | 115 | wp_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() | 118 | wp_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() | 118 | wp_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() | 125 | wp_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() | 128 | wp_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 138 | 71–128 | 5 | |
| 8.5 | 138 | 71–128 | 5 | |
| 8.4 | 138 | 71–128 | 5 | 2 more instructions than PHP 8.3 |
| 8.3 | 136 | 69–125 | 5 | |
| 8.2 | 136 | 69–125 | 5 | |
| 8.1 | 136 | 69–125 | 5 | |
| 7.4 | 136 | 69–125 | 5 |
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:
- apply_filters( duplicate_comment_id )filterline 788 (+36 into the body)
Filters the ID, if any, of the duplicate comment found when creating a new comment.
- do_action( comment_duplicate_trigger )actionline 798 (+46 into the body)
Fires immediately after a duplicate comment is detected.
- apply_filters( comment_duplicate_message )filterline 807 (+55 into the body)
Filters duplicate comment error message.
- do_action( check_comment_flood )actionline 835 (+83 into the body)
Fires immediately before a comment is marked approved.
- apply_filters( wp_is_comment_flood )filterline 858 (+106 into the body)
Filters whether a comment is part of a comment flood.
- apply_filters( comment_flood_message )filterline 869 (+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
- WP_REST_Comments_Controller::create_item()Creates a comment.
- wp_new_comment()Adds a new comment to the database.
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.
Signature, return type and hooks compared across 5 parsed releases.
$avoid_die parameter was renamed to $wp_error.from the docblock$avoid_die parameter was added, allowing the function to return a WP_Error object instead of dying.from the docblockAbout 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.