wp_new_comment( array $commentdata, bool $wp_error = false ): int|false|WP_Error
- Since
- 1.5.0, 4.3.0, 4.7.0, 5.5.0, 5.5.0
- Source
wp-includes/comment.php:2229
Description
Filters new comment to ensure that the fields are sanitized and valid before inserting comment into database. Calls 'comment_post' action with comment ID and whether comment is approved by WordPress. Also has 'preprocess_comment' filter for processing the comment data before the function handles it.
We use REMOTE_ADDR here directly. If you are behind a proxy, you should ensure that it is properly set, such as in wp-config.php, for your environment.
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- Comment data.
$comment_authorstringThe name of the comment author.$comment_author_emailstringThe comment author email address.$comment_author_urlstringThe comment author URL.$comment_contentstringThe content of the comment.$comment_datestringdefault: is the current timeThe date the comment was submitted.$comment_date_gmtstringdefault: is $comment_date in the GMT timezoneThe date the comment was submitted in the GMT timezone.$comment_typestringdefault: 'comment'Comment type.$comment_parentintdefault: 0The ID of this comment's parent, if any.$comment_post_IDintThe ID of the post that relates to the comment.$user_idintdefault: 0The ID of the user who submitted the comment.$user_IDintKept for backward-compatibility. Use$user_idinstead.$comment_agentstringComment author user agent. Default is the value of 'HTTP_USER_AGENT' in the$_SERVERsuperglobal sent in the original request.$comment_author_IPstringComment author IP address in IPv4 format. Default is the value of 'REMOTE_ADDR' in the$_SERVERsuperglobal sent in the original request.
$wp_errorbooloptional- Should errors be returned as WP_Error objects instead of executing wp_die()? Default false.Default:
false
Return value
int|false|WP_Error- The ID of the comment on success, false or WP_Error on failure.
Performance profile
How much work a call to wp_new_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
- 74–162
- Plugin surface
- 2 hooks
- Called by
- 4
Reads stored settings via get_option(), cached per request but not free on a cold cache.
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 229.
Third-party callbacks on 'preprocess_comment', 'comment_post' run inside this call, and their cost is not bounded by anything here.
4 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 - optionoption read or write
get_option()one call below wp_new_comment() - cacheobject cache
wp_cache_delete_multiple()one call below wp_new_comment()
Further down the call graph this can also reach serialize, query 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 · 132 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_new_comment() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!empty($commentdata) && is_wp_error() | 74–85 | apply_filters(), wp_allow_comment(), is_wp_error() |
is_wp_error() | 79–91 | apply_filters(), current_time(), wp_allow_comment(), is_wp_error() |
!empty($commentdata) && is_wp_error() | 80–91 | apply_filters(), wp_get_comment_status(), wp_allow_comment(), is_wp_error() |
!empty($commentdata) && is_wp_error() | 80–91 | apply_filters(), absint(), wp_allow_comment(), is_wp_error() |
empty($commentdata) && is_wp_error() | 85–96 | apply_filters(), current_time(), current_time(), wp_allow_comment(), is_wp_error() |
is_wp_error() | 85–97 | apply_filters(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error() |
is_wp_error() | 85–97 | apply_filters(), absint(), current_time(), wp_allow_comment(), is_wp_error() |
!empty($commentdata) && is_wp_error() | 86–97 | apply_filters(), absint(), wp_get_comment_status(), wp_allow_comment(), is_wp_error() |
!empty($commentdata) | 87–98 | apply_filters(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error() |
empty($commentdata) && is_wp_error() | 91–102 | apply_filters(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error() |
empty($commentdata) && is_wp_error() | 91–102 | apply_filters(), absint(), current_time(), current_time(), wp_allow_comment(), is_wp_error() |
is_wp_error() | 91–103 | apply_filters(), absint(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error() |
120 further outcomes, up to 162 instructions
!empty($commentdata) | 92–103 | apply_filters(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error() |
| always | 92–104 | apply_filters(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error() |
!empty($commentdata) | 93–104 | apply_filters(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error() |
!empty($commentdata) | 93–104 | apply_filters(), absint(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error() |
| always | 97–109 | apply_filters(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error() |
empty($commentdata) && is_wp_error() | 97–108 | apply_filters(), absint(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error() |
empty($commentdata) | 98–109 | apply_filters(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error() |
!empty($commentdata) | 98–109 | apply_filters(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error() |
| always | 98–110 | apply_filters(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error() |
!empty($commentdata) | 98–109 | apply_filters(), absint(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error() |
| always | 98–110 | apply_filters(), absint(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error() |
!empty($commentdata) && !is_wp_error() | 99–110 | apply_filters(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), do_action() |
!empty($commentdata) | 99–110 | apply_filters(), absint(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error() |
empty($commentdata) | 103–114 | apply_filters(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error() |
| always | 103–115 | apply_filters(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error() |
| always | 103–115 | apply_filters(), absint(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error() |
!empty($commentdata) && !is_wp_error() | 104–115 | apply_filters(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), do_action() |
!is_wp_error() | 104–116 | apply_filters(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), do_action() |
empty($commentdata) | 104–115 | apply_filters(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error() |
empty($commentdata) | 104–115 | apply_filters(), absint(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error() |
!empty($commentdata) | 104–115 | apply_filters(), absint(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error() |
| always | 104–116 | apply_filters(), absint(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error() |
!empty($commentdata) && !is_wp_error() | 105–116 | apply_filters(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), do_action() |
!empty($commentdata) && !is_wp_error() | 105–116 | apply_filters(), absint(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), do_action() |
!is_wp_error() | 109–121 | apply_filters(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), do_action() |
empty($commentdata) | 109–120 | apply_filters(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error() |
empty($commentdata) | 109–120 | apply_filters(), absint(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error() |
| always | 109–121 | apply_filters(), absint(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error() |
!empty($commentdata) | 110–122 | apply_filters(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
empty($commentdata) && !is_wp_error() | 110–121 | apply_filters(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), do_action() |
!empty($commentdata) && !is_wp_error() | 110–121 | apply_filters(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), do_action() |
!is_wp_error() | 110–122 | apply_filters(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), do_action() |
!empty($commentdata) && !is_wp_error() | 110–121 | apply_filters(), absint(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), do_action() |
!is_wp_error() | 110–122 | apply_filters(), absint(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), do_action() |
empty($commentdata) | 110–121 | apply_filters(), absint(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error() |
!empty($commentdata) && !is_wp_error() | 111–122 | apply_filters(), absint(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), do_action() |
!empty($commentdata) && !is_wp_error() | 114–126 | apply_filters(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
!empty($commentdata) | 115–127 | apply_filters(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
| always | 115–128 | apply_filters(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
empty($commentdata) && !is_wp_error() | 115–126 | apply_filters(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), do_action() |
!is_wp_error() | 115–127 | apply_filters(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), do_action() |
!is_wp_error() | 115–127 | apply_filters(), absint(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), do_action() |
empty($commentdata) | 115–126 | apply_filters(), absint(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error() |
!empty($commentdata) | 116–128 | apply_filters(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
empty($commentdata) && !is_wp_error() | 116–127 | apply_filters(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), do_action() |
!empty($commentdata) | 116–128 | apply_filters(), absint(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
empty($commentdata) && !is_wp_error() | 116–127 | apply_filters(), absint(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), do_action() |
!empty($commentdata) && !is_wp_error() | 116–127 | apply_filters(), absint(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), do_action() |
!is_wp_error() | 116–128 | apply_filters(), absint(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), do_action() |
!empty($commentdata) && !is_wp_error() | 119–131 | apply_filters(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
!is_wp_error() | 119–132 | apply_filters(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
| always | 120–133 | apply_filters(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
!empty($commentdata) && !is_wp_error() | 120–132 | apply_filters(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
!empty($commentdata) && !is_wp_error() | 120–132 | apply_filters(), absint(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
empty($commentdata) | 121–133 | apply_filters(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
!empty($commentdata) | 121–133 | apply_filters(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
| always | 121–134 | apply_filters(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
empty($commentdata) && !is_wp_error() | 121–132 | apply_filters(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), do_action() |
!empty($commentdata) | 121–133 | apply_filters(), absint(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
| always | 121–134 | apply_filters(), absint(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
empty($commentdata) && !is_wp_error() | 121–132 | apply_filters(), absint(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), do_action() |
!is_wp_error() | 121–133 | apply_filters(), absint(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), do_action() |
!empty($commentdata) && !is_wp_error() | 122–134 | apply_filters(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
!empty($commentdata) | 122–134 | apply_filters(), absint(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
empty($commentdata) && !is_wp_error() | 122–133 | apply_filters(), absint(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), do_action() |
!is_wp_error() | 124–137 | apply_filters(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
empty($commentdata) && !is_wp_error() | 125–137 | apply_filters(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
!empty($commentdata) && !is_wp_error() | 125–137 | apply_filters(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
!is_wp_error() | 125–138 | apply_filters(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
!empty($commentdata) && !is_wp_error() | 125–137 | apply_filters(), absint(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
!is_wp_error() | 125–138 | apply_filters(), absint(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
empty($commentdata) | 126–138 | apply_filters(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
| always | 126–139 | apply_filters(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
| always | 126–139 | apply_filters(), absint(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
!empty($commentdata) && !is_wp_error() | 126–138 | apply_filters(), absint(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
!empty($commentdata) && !is_wp_error() | 127–139 | apply_filters(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
!is_wp_error() | 127–140 | apply_filters(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
empty($commentdata) | 127–139 | apply_filters(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
empty($commentdata) | 127–139 | apply_filters(), absint(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
!empty($commentdata) | 127–139 | apply_filters(), absint(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
| always | 127–140 | apply_filters(), absint(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
empty($commentdata) && !is_wp_error() | 127–138 | apply_filters(), absint(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), do_action() |
!empty($commentdata) && !is_wp_error() | 128–140 | apply_filters(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
!empty($commentdata) && !is_wp_error() | 128–140 | apply_filters(), absint(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
empty($commentdata) && !is_wp_error() | 130–142 | apply_filters(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
!is_wp_error() | 130–143 | apply_filters(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
!is_wp_error() | 130–143 | apply_filters(), absint(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
empty($commentdata) && !is_wp_error() | 131–143 | apply_filters(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
empty($commentdata) && !is_wp_error() | 131–143 | apply_filters(), absint(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
!empty($commentdata) && !is_wp_error() | 131–143 | apply_filters(), absint(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
!is_wp_error() | 131–144 | apply_filters(), absint(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
!is_wp_error() | 132–145 | apply_filters(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
empty($commentdata) | 132–144 | apply_filters(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
empty($commentdata) | 132–144 | apply_filters(), absint(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
| always | 132–145 | apply_filters(), absint(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
empty($commentdata) && !is_wp_error() | 133–145 | apply_filters(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
!empty($commentdata) && !is_wp_error() | 133–145 | apply_filters(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
!is_wp_error() | 133–146 | apply_filters(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
!empty($commentdata) && !is_wp_error() | 133–145 | apply_filters(), absint(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
!is_wp_error() | 133–146 | apply_filters(), absint(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
empty($commentdata) | 133–145 | apply_filters(), absint(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
!empty($commentdata) && !is_wp_error() | 134–146 | apply_filters(), absint(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
empty($commentdata) && !is_wp_error() | 136–148 | apply_filters(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
empty($commentdata) && !is_wp_error() | 136–148 | apply_filters(), absint(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
!is_wp_error() | 136–149 | apply_filters(), absint(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
empty($commentdata) && !is_wp_error() | 137–149 | apply_filters(), absint(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
empty($commentdata) && !is_wp_error() | 138–150 | apply_filters(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
!is_wp_error() | 138–151 | apply_filters(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
!is_wp_error() | 138–151 | apply_filters(), absint(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
empty($commentdata) | 138–150 | apply_filters(), absint(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error() |
empty($commentdata) && !is_wp_error() | 139–151 | apply_filters(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
empty($commentdata) && !is_wp_error() | 139–151 | apply_filters(), absint(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
!empty($commentdata) && !is_wp_error() | 139–151 | apply_filters(), absint(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
!is_wp_error() | 139–152 | apply_filters(), absint(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
empty($commentdata) && !is_wp_error() | 142–154 | apply_filters(), absint(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment() |
empty($commentdata) && !is_wp_error() | 144–156 | apply_filters(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
empty($commentdata) && !is_wp_error() | 144–156 | apply_filters(), absint(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
!is_wp_error() | 144–157 | apply_filters(), absint(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
empty($commentdata) && !is_wp_error() | 145–157 | apply_filters(), absint(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
empty($commentdata) && !is_wp_error() | 150–162 | apply_filters(), absint(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error(), wp_insert_comment(), wp_filter_comment(), wp_allow_comment(), is_wp_error(), wp_insert_comment(), do_action() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 227 | 74–160 | 25 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 229 | 74–162 | 25 | |
| 8.4 | 229 | 74–162 | 25 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 235 | 80–168 | 25 | |
| 8.2 | 235 | 80–168 | 25 | |
| 8.1 | 235 | 80–168 | 25 | 3 fewer instructions than PHP 7.4 |
| 7.4 | 238 | 81–169 | 25 |
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 · 2
2 hooks fire while wp_new_comment() runs, in this order:
- apply_filters( preprocess_comment )filterline 2262 (+33 into the body)
Filters a comment's data before it is sanitized and inserted into the database.
- do_action( comment_post )actionline 2348 (+119 into the body)
Fires immediately after a comment is inserted into the database.
Uses · 10
- apply_filters()Calls the callback functions that have been added to a filter hook.
- absint()Converts a value to non-negative integer.
- wp_get_comment_status()Retrieves the status of a comment by comment ID.
- current_time()Retrieves the current time based on specified type.
- wp_allow_comment()Validates whether this comment is allowed to be made.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_filter_comment()Filters and sanitizes comment data.
- wp_check_comment_data()Checks whether comment data passes internal checks or has disallowed content.
- wp_insert_comment()Inserts a comment into the database.
- do_action()Calls the callback functions that have been added to an action hook.
Used by · 4
- wp_ajax_replyto_comment()Handles replying to a comment via AJAX.
- wp_handle_comment_submission()Handles the submission of a comment, usually posted to wp-comments-post.php via a comment form.
- wp_xmlrpc_server::pingback_ping()Retrieves a pingback and registers it.
- wp_xmlrpc_server::wp_newComment()Creates a new comment.
Source code
function wp_new_comment( $commentdata, $wp_error = false ) { global $wpdb; /* * Normalize `user_ID` to `user_id`, but pass the old key * to the `preprocess_comment` filter for backward compatibility. */ if ( isset( $commentdata['user_ID'] ) ) { $commentdata['user_ID'] = (int) $commentdata['user_ID']; $commentdata['user_id'] = $commentdata['user_ID']; } elseif ( isset( $commentdata['user_id'] ) ) { $commentdata['user_id'] = (int) $commentdata['user_id']; $commentdata['user_ID'] = $commentdata['user_id']; } $prefiltered_user_id = ( isset( $commentdata['user_id'] ) ) ? (int) $commentdata['user_id'] : 0; if ( ! isset( $commentdata['comment_author_IP'] ) ) { $commentdata['comment_author_IP'] = $_SERVER['REMOTE_ADDR']; } if ( ! isset( $commentdata['comment_agent'] ) ) { $commentdata['comment_agent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : ''; } /** * Filters a comment's data before it is sanitized and inserted into the database. * * @since 1.5.0 * @since 5.6.0 Comment data includes the `comment_agent` and `comment_author_IP` values. * * @param array $commentdata Comment data. */ $commentdata = apply_filters( 'preprocess_comment', $commentdata ); $commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID']; // Normalize `user_ID` to `user_id` again, after the filter. if ( isset( $commentdata['user_ID'] ) && $prefiltered_user_id !== (int) $commentdata['user_ID'] ) { $commentdata['user_ID'] = (int) $commentdata['user_ID']; $commentdata['user_id'] = $commentdata['user_ID']; } elseif ( isset( $commentdata['user_id'] ) ) { $commentdata['user_id'] = (int) $commentdata['user_id']; $commentdata['user_ID'] = $commentdata['user_id']; } $commentdata['comment_parent'] = isset( $commentdata['comment_parent'] ) ? absint( $commentdata['comment_parent'] ) : 0; $parent_status = ( $commentdata['comment_parent'] > 0 ) ? wp_get_comment_status( $commentdata['comment_parent'] ) : ''; $commentdata['comment_parent'] = ( 'approved' === $parent_status || 'unapproved' === $parent_status ) ? $commentdata['comment_parent'] : 0; $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '', $commentdata['comment_author_IP'] ); $commentdata['comment_agent'] = substr( $commentdata['comment_agent'], 0, 254 ); if ( empty( $commentdata['comment_date'] ) ) { $commentdata['comment_date'] = current_time( 'mysql' ); } if ( empty( $commentdata['comment_date_gmt'] ) ) { $commentdata['comment_date_gmt'] = current_time( 'mysql', 1 ); } if ( empty( $commentdata['comment_type'] ) ) { $commentdata['comment_type'] = 'comment'; } $commentdata['comment_approved'] = wp_allow_comment( $commentdata, $wp_error ); if ( is_wp_error( $commentdata['comment_approved'] ) ) { return $commentdata['comment_approved']; } $commentdata = wp_filter_comment( $commentdata ); if ( ! in_array( $commentdata['comment_approved'], array( 'trash', 'spam' ), true ) ) { // Validate the comment again after filters are applied to comment data. $commentdata['comment_approved'] = wp_check_comment_data( $commentdata ); }Changelog
Introduced in 5.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
comment_type argument.from the docblock$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 docblockcomment_agent and comment_author_IP arguments.from the docblockAbout 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.