check_comment( string $author, string $email, string $url, string $comment, string $user_ip, string $user_agent, string $comment_type ): bool
- Since
- 1.2.0, 7.1.0
- Source
wp-includes/comment.php:43
Description
If manual comment moderation is set in the administration, then all checks, regardless of their type and substance, will fail and the function will return false.
If the number of links exceeds the amount in the administration, then the check fails. If any of the parameter contents contain any disallowed words, then the check fails.
If the comment author was approved before, then the comment is automatically approved.
Pingbacks originating from the same site are automatically approved, as the link they report was created by someone who can already publish here.
If all checks pass, the function will return true.
Compatibility
- WordPress
- since 7.1.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
$authorstring- Comment author name.
$emailstring- Comment author email.
$urlstring- Comment author URL.
$commentstring- Content of the comment.
$user_ipstring- Comment author IP address.
$user_agentstring- Comment author User-Agent.
$comment_typestring- Comment type, either user-submitted comment, trackback, or pingback.
Return value
bool- If all checks pass, true, otherwise false.
Performance profile
How much work a call to check_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
- Scales with input
- Instructions
- 14–99
- Plugin surface
- 3 hooks
- Called by
- 1
Reaches the database via ->get_var().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 178.
Third-party callbacks on 'comment_text', 'comment_max_links_url', 'wp_auto_approve_ping' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()called directly - querycontent query
get_user_by()called directly - regexregular expression over the whole input
preg_match_all()called directly - sqldatabase query
->get_var()called directly - cacheobject cache
wp_cache_get()one call below check_comment() - serializeserialisation
maybe_unserialize()one call below check_comment()
Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 20 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost check_comment() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 14 | get_option() |
empty($mod_keys) | 37–47 | get_option(), apply_filters(), get_option(), get_option(), get_option() |
$max_links | 40 | get_option(), apply_filters(), get_option(), preg_match_all(), apply_filters() |
!empty($mod_keys) | 45–56 | get_option(), apply_filters(), get_option(), get_option(), explode(), get_option() |
!$max_links && empty($mod_keys) | 51–61 | get_option(), apply_filters(), get_option(), preg_match_all(), apply_filters(), get_option(), get_option() |
!empty($mod_keys) && !$words && !empty($word) && $pattern | 56–66 | get_option(), apply_filters(), get_option(), get_option(), explode(), preg_quote() |
empty($mod_keys) && $comment_type === "pingback" && !$source_id | 58–64 | get_option(), apply_filters(), get_option(), get_option(), get_option(), wp_unslash(), url_to_postid(), apply_filters() |
!$max_links && !empty($mod_keys) | 59–70 | get_option(), apply_filters(), get_option(), preg_match_all(), apply_filters(), get_option(), explode(), get_option() |
empty($mod_keys) && $comment_type === "pingback" && $source_id | 62–68 | get_option(), apply_filters(), get_option(), get_option(), get_option(), wp_unslash(), url_to_postid(), get_post_status(), apply_filters() |
!empty($mod_keys) && $comment_type === "pingback" && !$source_id | 66–73 | get_option(), apply_filters(), get_option(), get_option(), explode(), get_option(), wp_unslash(), url_to_postid(), apply_filters() |
!$max_links && !empty($mod_keys) && !$words && !empty($word) && $pattern | 70–80 | get_option(), apply_filters(), get_option(), preg_match_all(), apply_filters(), get_option(), explode(), preg_quote() |
empty($mod_keys) && $comment_type !== "trackback" && $comment_type !== "pingback" && $author !== "" && $email !== "" | 70–76 | get_option(), apply_filters(), get_option(), get_option(), get_option(), wp_unslash(), get_user_by(), ->prepare(), ->get_var() |
8 further outcomes, up to 99 instructions
!empty($mod_keys) && $comment_type === "pingback" && $source_id | 70–77 | get_option(), apply_filters(), get_option(), get_option(), explode(), get_option(), wp_unslash(), url_to_postid(), get_post_status(), apply_filters() |
!$max_links && empty($mod_keys) && $comment_type === "pingback" && !$source_id | 72–78 | get_option(), apply_filters(), get_option(), preg_match_all(), apply_filters(), get_option(), get_option(), wp_unslash(), url_to_postid(), apply_filters() |
!$max_links && empty($mod_keys) && $comment_type === "pingback" && $source_id | 76–82 | get_option(), apply_filters(), get_option(), preg_match_all(), apply_filters(), get_option(), get_option(), wp_unslash(), url_to_postid(), get_post_status(), apply_filters() |
!empty($mod_keys) && $comment_type !== "trackback" && $comment_type !== "pingback" && $author !== "" && $email !== "" | 78–85 | get_option(), apply_filters(), get_option(), get_option(), explode(), get_option(), wp_unslash(), get_user_by(), ->prepare(), ->get_var() |
!$max_links && !empty($mod_keys) && $comment_type === "pingback" && !$source_id | 80–87 | get_option(), apply_filters(), get_option(), preg_match_all(), apply_filters(), get_option(), explode(), get_option(), wp_unslash(), url_to_postid(), apply_filters() |
!$max_links && empty($mod_keys) && $comment_type !== "trackback" && $comment_type !== "pingback" && $author !== "" && $email !== "" | 84–90 | get_option(), apply_filters(), get_option(), preg_match_all(), apply_filters(), get_option(), get_option(), wp_unslash(), get_user_by(), ->prepare(), ->get_var() |
!$max_links && !empty($mod_keys) && $comment_type === "pingback" && $source_id | 84–91 | get_option(), apply_filters(), get_option(), preg_match_all(), apply_filters(), get_option(), explode(), get_option(), wp_unslash(), url_to_postid(), get_post_status(), apply_filters() |
!$max_links && !empty($mod_keys) && $comment_type !== "trackback" && $comment_type !== "pingback" && $author !== "" && $email !== "" | 92–99 | get_option(), apply_filters(), get_option(), preg_match_all(), apply_filters(), get_option(), explode(), get_option(), wp_unslash(), get_user_by(), ->prepare(), ->get_var() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 178 | 14–99 | 24 | |
| 8.5 | 178 | 14–99 | 24 | |
| 8.4 | 178 | 14–99 | 24 | 26 fewer instructions than PHP 8.3 |
| 8.3 | 204 | 14–105 | 24 | |
| 8.2 | 204 | 14–105 | 24 | |
| 8.1 | 204 | 14–105 | 24 | |
| 7.4 | 204 | 14–105 | 24 |
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 · 3
3 hooks fire while check_comment() runs, in this order:
- apply_filters( comment_text )filterline 52 (+9 into the body)
Filters the text of a comment to be displayed.
- apply_filters( comment_max_links_url )filterline 69 (+26 into the body)
Filters the number of links found in a comment.
- apply_filters( wp_auto_approve_ping )filterline 197 (+154 into the body)
Filters whether a pingback is approved without being held for moderation.
Uses · 7
- get_option()Retrieves an option value based on an option name.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_user_by()Retrieves user info by a given field.
- wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- url_to_postid()Examines a URL and try to determine the post ID it represents.
- get_post_status()Retrieves the post status based on the post ID.
Used by · 1
- wp_check_comment_data()Checks whether comment data passes internal checks or has disallowed content.
Source code
function check_comment( $author, $email, $url, $comment, $user_ip, $user_agent, $comment_type ) { global $wpdb; // If manual moderation is enabled, skip all checks and return false. if ( '1' === get_option( 'comment_moderation' ) ) { return false; } /** This filter is documented in wp-includes/comment-template.php */ $comment = apply_filters( 'comment_text', $comment, null, array() ); // Check for the number of external links if a max allowed number is set. $max_links = get_option( 'comment_max_links' ); if ( $max_links ) { $num_links = preg_match_all( '/<a [^>]*href/i', $comment, $out ); /** * Filters the number of links found in a comment. * * @since 3.0.0 * @since 4.7.0 Added the `$comment` parameter. * * @param int $num_links The number of links found. * @param string $url Comment author's URL. Included in allowed links total. * @param string $comment Content of the comment. */ $num_links = apply_filters( 'comment_max_links_url', $num_links, $url, $comment ); /* * If the number of links in the comment exceeds the allowed amount, * fail the check by returning false. */ if ( $num_links >= $max_links ) { return false; } } $mod_keys = trim( get_option( 'moderation_keys' ) ); // If moderation 'keys' (keywords) are set, process them. if ( ! empty( $mod_keys ) ) { $words = explode( "\n", $mod_keys ); foreach ( (array) $words as $word ) { $word = trim( $word ); // Skip empty lines. if ( empty( $word ) ) { continue; } /* * Do some escaping magic so that '#' (number of) characters in the spam * words don't break things: */ $word = preg_quote( $word, '#' ); /* * Check the comment fields for moderation keywords. If any are found, * fail the check for the given field by returning false. */ $pattern = "#$word#iu"; if ( preg_match( $pattern, $author ) ) { return false; } if ( preg_match( $pattern, $email ) ) { return false; } if ( preg_match( $pattern, $url ) ) { return false; } if ( preg_match( $pattern, $comment ) ) { return false; } if ( preg_match( $pattern, $user_ip ) ) { return false; } if ( preg_match( $pattern, $user_agent ) ) { return false; }Changelog
Introduced in 1.2.0. Unchanged from 6.7.7 through 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.