wppaste
WordPress

wp_insert_comment( array $commentdata ): int|false

Since
2.0.0, 4.4.0, 5.5.0
Source
wp-includes/comment.php:2013
Inserts a comment into the database.

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
Array of arguments for inserting a new comment.
  • $comment_agentstringdefault: empty

    The HTTP user agent of the $comment_author when the comment was submitted.
  • $comment_approvedint|stringdefault: 1

    Whether the comment has been approved.
  • $comment_authorstringdefault: empty

    The name of the author of the comment.
  • $comment_author_emailstringdefault: empty

    The email address of the $comment_author.
  • $comment_author_IPstringdefault: empty

    The IP address of the $comment_author.
  • $comment_author_urlstringdefault: empty

    The URL address of the $comment_author.
  • $comment_contentstringdefault: empty

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

    The date the comment was submitted. To set the date manually, $comment_date_gmt must also be specified.
  • $comment_date_gmtstringdefault: is $comment_date in the site's GMT timezone

    The date the comment was submitted in the GMT timezone.
  • $comment_karmaintdefault: 0

    The karma of the comment.
  • $comment_parentintdefault: 0

    ID of this comment's parent, if any.
  • $comment_post_IDintdefault: 0

    ID of the post that relates to the comment, if any.
  • $comment_typestringdefault: 'comment'

    Comment type.
  • $comment_metaarray

    Optional. Array of key/value pairs to be stored in commentmeta for the new comment.
  • $user_idintdefault: 0

    ID of the user who submitted the comment.

Return value

int|false
The new comment's ID on success, false on failure.

Performance profile

How much work a call to wp_insert_comment() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via ->insert().

Scaling
Scales with input

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

Instructions
88–134

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

Plugin surface
1 hook

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

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • cacheobject cachewp_cache_delete_multiple()called directly
  • hookthird-party callbacksdo_action()called directly
  • sqldatabase query->insert()called directly
  • optionoption read or writeget_option()one call below wp_insert_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 · 6 distinct outcomes

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

WhenInstructionsCalls it makes
isset($data) && !->insert()88–96wp_unslash(), compact(), ->insert()
!->insert()92–97wp_unslash(), get_gmt_from_date(), compact(), ->insert()
isset($data) && ->insert() && $comment_approved != 1107–122wp_unslash(), compact(), ->insert(), clean_comment_cache(), get_comment(), do_action()
->insert() && $comment_approved != 1111–123wp_unslash(), get_gmt_from_date(), compact(), ->insert(), clean_comment_cache(), get_comment(), do_action()
isset($data) && ->insert() && $comment_approved == 1117–133wp_unslash(), compact(), ->insert(), wp_update_comment_count(), wp_cache_delete_multiple(), clean_comment_cache(), get_comment(), do_action()
->insert() && $comment_approved == 1121–134wp_unslash(), get_gmt_from_date(), compact(), ->insert(), wp_update_comment_count(), wp_cache_delete_multiple(), clean_comment_cache(), get_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-dev17288–133222 fewer instructions than PHP 8.5
8.517488–13422
8.417488–13422
8.317488–13422
8.217488–13422
8.117488–1342214 fewer instructions than PHP 7.4
7.4188102–14222

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

One hook fires while wp_insert_comment() runs, in this order:

  1. do_action( wp_insert_comment )actionline 2091 (+78 into the body)

    Fires immediately after a comment is inserted into the database.

Uses · 9

Used by · 2

Source code

function wp_insert_comment( $commentdata ) {	global $wpdb; 	$data = wp_unslash( $commentdata ); 	$comment_author       = ! isset( $data['comment_author'] ) ? '' : $data['comment_author'];	$comment_author_email = ! isset( $data['comment_author_email'] ) ? '' : $data['comment_author_email'];	$comment_author_url   = ! isset( $data['comment_author_url'] ) ? '' : $data['comment_author_url'];	$comment_author_ip    = ! isset( $data['comment_author_IP'] ) ? '' : $data['comment_author_IP']; 	$comment_date     = ! isset( $data['comment_date'] ) ? current_time( 'mysql' ) : $data['comment_date'];	$comment_date_gmt = ! isset( $data['comment_date_gmt'] ) ? get_gmt_from_date( $comment_date ) : $data['comment_date_gmt']; 	$comment_post_id  = ! isset( $data['comment_post_ID'] ) ? 0 : $data['comment_post_ID'];	$comment_content  = ! isset( $data['comment_content'] ) ? '' : $data['comment_content'];	$comment_karma    = ! isset( $data['comment_karma'] ) ? 0 : $data['comment_karma'];	$comment_approved = ! isset( $data['comment_approved'] ) ? 1 : $data['comment_approved'];	$comment_agent    = ! isset( $data['comment_agent'] ) ? '' : $data['comment_agent'];	$comment_type     = empty( $data['comment_type'] ) ? 'comment' : $data['comment_type'];	$comment_parent   = ! isset( $data['comment_parent'] ) ? 0 : $data['comment_parent']; 	$user_id = ! isset( $data['user_id'] ) ? 0 : $data['user_id']; 	$compacted = array(		'comment_post_ID'   => $comment_post_id,		'comment_author_IP' => $comment_author_ip,	); 	$compacted += compact(		'comment_author',		'comment_author_email',		'comment_author_url',		'comment_date',		'comment_date_gmt',		'comment_content',		'comment_karma',		'comment_approved',		'comment_agent',		'comment_type',		'comment_parent',		'user_id'	); 	if ( ! $wpdb->insert( $wpdb->comments, $compacted ) ) {		return false;	} 	$id = (int) $wpdb->insert_id; 	if ( 1 == $comment_approved ) {		wp_update_comment_count( $comment_post_id ); 		$data = array();		foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) {			$data[] = "lastcommentmodified:$timezone";		}		wp_cache_delete_multiple( $data, 'timeinfo' );	} 	clean_comment_cache( $id ); 	$comment = get_comment( $id ); 	// If metadata is provided, store it.	if ( isset( $commentdata['comment_meta'] ) && is_array( $commentdata['comment_meta'] ) ) {		foreach ( $commentdata['comment_meta'] as $meta_key => $meta_value ) {			add_comment_meta( $comment->comment_ID, $meta_key, $meta_value, true );		}	} 	/**	 * Fires immediately after a comment is inserted into the database.	 *	 * @since 2.8.0	 *	 * @param int        $id      The comment ID.	 * @param WP_Comment $comment Comment object.	 */	do_action( 'wp_insert_comment', $id, $comment );

Changelog

Introduced in 4.4.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
Default value for $comment_type argument changed to comment.from the docblock
4.4.0
Introduced the $comment_meta argument.from the docblock
2.0.0
Introduced.from the docblock

About 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.