wp_set_comment_status( int|WP_Comment $comment_id, string $comment_status, bool $wp_error = false ): bool|WP_Error
- Since
- 1.0.0
- Source
wp-includes/comment.php:2795
Description
The 'wp_set_comment_status' action is called after the comment is handled.
If the comment status is not in the list, then false is returned.
Compatibility
- WordPress
- since 1.0.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
$comment_idint|WP_Comment- Comment ID or WP_Comment object.
$comment_statusstring- New comment status, either 'hold', 'approve', 'spam', or 'trash'.
$wp_errorbooloptional- Whether to return a WP_Error object if there is a failure. Default false.Default:
false
Return value
bool|WP_Error- True on success, false or WP_Error on failure.
Performance profile
How much work a call to wp_set_comment_status() 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
- Light
- Scaling
- Constant
- Instructions
- 17–64
- Plugin surface
- 1 hook
- Called by
- 7
Only touches the object cache, via wp_cache_delete_multiple().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 87.
Third-party callbacks on 'wp_set_comment_status' run inside this call, and their cost is not bounded by anything here.
7 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()called directly - cacheobject cache
wp_cache_delete_multiple()one call below wp_set_comment_status()
Further down the call graph this can also reach query, option, serialize 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 · 7 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_set_comment_status() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$comment_status != "hold" && $comment_status != "0" && $comment_status != "approve" && $comment_status != "1" && $comment_status != "spam" && $comment_status != "trash" | 17 | none |
!comment_ID() | 25–34 | get_comment(), comment_ID() |
$comment_status != "hold" && $comment_status != "0" && !comment_ID() | 33–35 | add_action(), get_comment(), comment_ID() |
!comment_ID() | 35–44 | get_comment(), comment_ID(), __(), comment_approved() |
$comment_status != "hold" && $comment_status != "0" && !comment_ID() | 43–45 | add_action(), get_comment(), comment_ID(), __(), comment_approved() |
comment_ID() | 54–63 | get_comment(), comment_ID(), clean_comment_cache(), get_comment(), do_action(), wp_transition_comment_status(), wp_update_comment_count() |
$comment_status != "hold" && $comment_status != "0" && comment_ID() | 62–64 | add_action(), get_comment(), comment_ID(), clean_comment_cache(), get_comment(), do_action(), wp_transition_comment_status(), wp_update_comment_count() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 87 | 17–64 | 8 | |
| 8.5 | 87 | 17–64 | 8 | |
| 8.4 | 87 | 17–64 | 8 | |
| 8.3 | 87 | 17–64 | 8 | |
| 8.2 | 87 | 17–64 | 8 | |
| 8.1 | 87 | 17–64 | 8 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 88 | 17–64 | 8 |
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_set_comment_status() runs, in this order:
- do_action( wp_set_comment_status )actionline 2842 (+47 into the body)
Fires immediately after transitioning a comment's status from one to another in the database and removing the comment from the object cache, but prior to all status transition hooks.
Uses · 8
- add_action()Adds a callback function to an action hook.
- get_comment()Retrieves comment data given a comment ID or comment object.
- __()Retrieves the translation of $text.
- clean_comment_cache()Removes a comment from the object cache.
- do_action()Calls the callback functions that have been added to an action hook.
- wp_transition_comment_status()Calls hooks for when a comment status transition occurs.
- wp_update_comment_count()Updates the comment count for post(s).
- WP_Error::__construct()Initializes the error.
Used by · 7
- WP_REST_Comments_Controller::handle_status_param()Sets the comment_status of a given comment object when creating or updating a comment.
- wp_ajax_dim_comment()Handles dimming a comment via AJAX.
- wp_ajax_replyto_comment()Handles replying to a comment via AJAX.
- wp_spam_comment()Marks a comment as Spam.
- wp_trash_comment()Moves a comment to the Trash
- wp_unspam_comment()Removes a comment from the Spam.
- wp_untrash_comment()Removes a comment from the Trash
Source code
function wp_set_comment_status( $comment_id, $comment_status, $wp_error = false ) { global $wpdb; switch ( $comment_status ) { case 'hold': case '0': $status = '0'; break; case 'approve': case '1': $status = '1'; add_action( 'wp_set_comment_status', 'wp_new_comment_notify_postauthor' ); break; case 'spam': $status = 'spam'; break; case 'trash': $status = 'trash'; break; default: return false; } $comment_old = clone get_comment( $comment_id ); if ( ! $wpdb->update( $wpdb->comments, array( 'comment_approved' => $status ), array( 'comment_ID' => $comment_old->comment_ID ) ) ) { if ( $wp_error ) { return new WP_Error( 'db_update_error', __( 'Could not update comment status.' ), $wpdb->last_error ); } else { return false; } } clean_comment_cache( $comment_old->comment_ID ); $comment = get_comment( $comment_old->comment_ID ); /** * Fires immediately after transitioning a comment's status from one to another in the database * and removing the comment from the object cache, but prior to all status transition hooks. * * @since 1.5.0 * * @param string $comment_id Comment ID as a numeric string. * @param string $comment_status Current comment status. Possible values include * 'hold', '0', 'approve', '1', 'spam', and 'trash'. */ do_action( 'wp_set_comment_status', $comment->comment_ID, $comment_status ); wp_transition_comment_status( $comment_status, $comment_old->comment_approved, $comment ); wp_update_comment_count( $comment->comment_post_ID ); return true;}Changelog
Introduced in 1.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/comment.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.