wppaste
WordPress

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:2314
Adds a new comment to the database.

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.

See https://core.trac.wordpress.org/ticket/9235

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_authorstring

    The name of the comment author.
  • $comment_author_emailstring

    The comment author email address.
  • $comment_author_urlstring

    The comment author URL.
  • $comment_contentstring

    The content of the comment.
  • $comment_datestringdefault: is the current time

    The date the comment was submitted.
  • $comment_date_gmtstringdefault: is $comment_date in the GMT timezone

    The date the comment was submitted in the GMT timezone.
  • $comment_typestringdefault: 'comment'

    Comment type.
  • $comment_parentintdefault: 0

    The ID of this comment's parent, if any.
  • $comment_post_IDint

    The ID of the post that relates to the comment.
  • $user_idintdefault: 0

    The ID of the user who submitted the comment.
  • $user_IDint

    Kept for backward-compatibility. Use $user_id instead.
  • $comment_agentstring

    Comment author user agent. Default is the value of 'HTTP_USER_AGENT' in the $_SERVER superglobal sent in the original request.
  • $comment_author_IPstring

    Comment author IP address in IPv4 format. Default is the value of 'REMOTE_ADDR' in the $_SERVER superglobal 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

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
74–162

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

Plugin surface
2 hooks

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

Called by
4

4 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
  • optionoption read or writeget_option()one call below wp_new_comment()
  • cacheobject cachewp_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.

WhenInstructionsCalls it makes
!empty($commentdata) && is_wp_error()74–85apply_filters(), wp_allow_comment(), is_wp_error()
is_wp_error()79–91apply_filters(), current_time(), wp_allow_comment(), is_wp_error()
!empty($commentdata) && is_wp_error()80–91apply_filters(), wp_get_comment_status(), wp_allow_comment(), is_wp_error()
!empty($commentdata) && is_wp_error()80–91apply_filters(), absint(), wp_allow_comment(), is_wp_error()
empty($commentdata) && is_wp_error()85–96apply_filters(), current_time(), current_time(), wp_allow_comment(), is_wp_error()
is_wp_error()85–97apply_filters(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error()
is_wp_error()85–97apply_filters(), absint(), current_time(), wp_allow_comment(), is_wp_error()
!empty($commentdata) && is_wp_error()86–97apply_filters(), absint(), wp_get_comment_status(), wp_allow_comment(), is_wp_error()
!empty($commentdata)87–98apply_filters(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error()
empty($commentdata) && is_wp_error()91–102apply_filters(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error()
empty($commentdata) && is_wp_error()91–102apply_filters(), absint(), current_time(), current_time(), wp_allow_comment(), is_wp_error()
is_wp_error()91–103apply_filters(), absint(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error()
120 further outcomes, up to 162 instructions
!empty($commentdata)92–103apply_filters(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error()
always92–104apply_filters(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error()
!empty($commentdata)93–104apply_filters(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error()
!empty($commentdata)93–104apply_filters(), absint(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error()
always97–109apply_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–108apply_filters(), absint(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error()
empty($commentdata)98–109apply_filters(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error()
!empty($commentdata)98–109apply_filters(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error()
always98–110apply_filters(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error()
!empty($commentdata)98–109apply_filters(), absint(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error()
always98–110apply_filters(), absint(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error()
!empty($commentdata) && !is_wp_error()99–110apply_filters(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), do_action()
!empty($commentdata)99–110apply_filters(), absint(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error()
empty($commentdata)103–114apply_filters(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error()
always103–115apply_filters(), wp_get_comment_status(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error()
always103–115apply_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–115apply_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–116apply_filters(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), do_action()
empty($commentdata)104–115apply_filters(), wp_get_comment_status(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error()
empty($commentdata)104–115apply_filters(), absint(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error()
!empty($commentdata)104–115apply_filters(), absint(), wp_get_comment_status(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error()
always104–116apply_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–116apply_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–116apply_filters(), absint(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), do_action()
!is_wp_error()109–121apply_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–120apply_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–120apply_filters(), absint(), current_time(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), wp_check_comment_data(), is_wp_error()
always109–121apply_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–122apply_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–121apply_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–121apply_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–122apply_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–121apply_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–122apply_filters(), absint(), current_time(), wp_allow_comment(), is_wp_error(), wp_filter_comment(), is_wp_error(), wp_insert_comment(), do_action()
empty($commentdata)110–121apply_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–122apply_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–126apply_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–127apply_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()
always115–128apply_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–126apply_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–127apply_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–127apply_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–126apply_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–128apply_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–127apply_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–128apply_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–127apply_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–127apply_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–128apply_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–131apply_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–132apply_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()
always120–133apply_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–132apply_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–132apply_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–133apply_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–133apply_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()
always121–134apply_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–132apply_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–133apply_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()
always121–134apply_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–132apply_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–133apply_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–134apply_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–134apply_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–133apply_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–137apply_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–137apply_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–137apply_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–138apply_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–137apply_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–138apply_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–138apply_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()
always126–139apply_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()
always126–139apply_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–138apply_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–139apply_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–140apply_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–139apply_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–139apply_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–139apply_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()
always127–140apply_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–138apply_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–140apply_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–140apply_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–142apply_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–143apply_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–143apply_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–143apply_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–143apply_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–143apply_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–144apply_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–145apply_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–144apply_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–144apply_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()
always132–145apply_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–145apply_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–145apply_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–146apply_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–145apply_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–146apply_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–145apply_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–146apply_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–148apply_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–148apply_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–149apply_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–149apply_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–150apply_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–151apply_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–151apply_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–150apply_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–151apply_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–151apply_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–151apply_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–152apply_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–154apply_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–156apply_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–156apply_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–157apply_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–157apply_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–162apply_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

PHPCompiledExecutedBranchesNotes
8.6-dev22474–160252 fewer instructions than PHP 8.5
8.522674–16225
8.422674–162256 fewer instructions than PHP 8.3
8.323280–16825
8.223280–16825
8.123280–168252 fewer instructions than PHP 7.4
7.423481–16925

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:

  1. apply_filters( preprocess_comment )filterline 2347 (+33 into the body)

    Filters a comment's data before it is sanitized and inserted into the database.

  2. do_action( comment_post )actionline 2433 (+119 into the body)

    Fires immediately after a comment is inserted into the database.

Uses · 10

Used by · 4

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'] = $_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', true );	} 	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.

  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
Introduced the comment_type argument.from the docblock
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
4.3.0
Introduced the comment_agent and comment_author_IP arguments.from the docblock
1.5.0
Introduced.from the docblock

About this page

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