wp_insert_comment( array $commentdata ): int|false
- Since
- 2.0.0, 4.4.0, 5.5.0
- Source
wp-includes/comment.php:2095
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: emptyThe HTTP user agent of the$comment_authorwhen the comment was submitted.$comment_approvedint|stringdefault: 1Whether the comment has been approved.$comment_authorstringdefault: emptyThe name of the author of the comment.$comment_author_emailstringdefault: emptyThe email address of the$comment_author.$comment_author_IPstringdefault: emptyThe IP address of the$comment_author.$comment_author_urlstringdefault: emptyThe URL address of the$comment_author.$comment_contentstringdefault: emptyThe content of the comment.$comment_datestringdefault: is the current timeThe date the comment was submitted. To set the date manually,$comment_date_gmtmust also be specified.$comment_date_gmtstringdefault: is $comment_date in the site's GMT timezoneThe date the comment was submitted in the GMT timezone.$comment_karmaintdefault: 0The karma of the comment.$comment_parentintdefault: 0ID of this comment's parent, if any.$comment_post_IDintdefault: 0ID of the post that relates to the comment, if any.$comment_typestringdefault: 'comment'Comment type.$comment_metaarrayOptional. Array of key/value pairs to be stored in commentmeta for the new comment.$user_idintdefault: 0ID 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
- Scaling
- Scales with input
- Instructions
- 88–135
- Plugin surface
- 1 hook
- Called by
- 2
Reaches the database via ->insert().
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 175.
Third-party callbacks on 'wp_insert_comment' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- cacheobject cache
wp_cache_delete_multiple()called directly - hookthird-party callbacks
do_action()called directly - sqldatabase query
->insert()called directly - optionoption read or write
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
isset($data) && !->insert() | 88–96 | wp_unslash(), compact(), ->insert() |
!->insert() | 92–97 | wp_unslash(), get_gmt_from_date(), compact(), ->insert() |
isset($data) && ->insert() | 108–123 | wp_unslash(), compact(), ->insert(), clean_comment_cache(), get_comment(), do_action() |
->insert() | 112–124 | wp_unslash(), get_gmt_from_date(), compact(), ->insert(), clean_comment_cache(), get_comment(), do_action() |
isset($data) && ->insert() | 118–134 | wp_unslash(), compact(), ->insert(), wp_update_comment_count(), wp_cache_delete_multiple(), clean_comment_cache(), get_comment(), do_action() |
->insert() | 122–135 | wp_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 173 | 88–134 | 22 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 175 | 88–135 | 22 | |
| 8.4 | 175 | 88–135 | 22 | |
| 8.3 | 175 | 88–135 | 22 | |
| 8.2 | 175 | 88–135 | 22 | |
| 8.1 | 175 | 88–135 | 22 | 14 fewer instructions than PHP 7.4 |
| 7.4 | 189 | 102–143 | 22 |
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:
- do_action( wp_insert_comment )actionline 2173 (+78 into the body)
Fires immediately after a comment is inserted into the database.
Uses · 9
- wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
- current_time()Retrieves the current time based on specified type.
- get_gmt_from_date()Given a date in the timezone of the site, returns that date in UTC.
- wp_update_comment_count()Updates the comment count for post(s).
- wp_cache_delete_multiple()Deletes multiple values from the cache in one call.
- clean_comment_cache()Removes a comment from the object cache.
- get_comment()Retrieves comment data given a comment ID or comment object.
- add_comment_meta()Adds meta data field to a comment.
- do_action()Calls the callback functions that have been added to an action hook.
Used by · 2
- WP_REST_Comments_Controller::create_item()Creates a comment.
- wp_new_comment()Adds a new comment to the database.
Source code
function wp_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 === (int) $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.
Signature, return type and hooks compared across 5 parsed releases.
$comment_type argument changed to comment.from the docblock$comment_meta argument.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.9.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.