wppaste
WordPress

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
Sets the status of a comment.

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

Only touches the object cache, via wp_cache_delete_multiple().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
17–64

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

Plugin surface
1 hook

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

Called by
7

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

What it touches

  • hookthird-party callbacksdo_action()called directly
  • cacheobject cachewp_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.

WhenInstructionsCalls it makes
$comment_status != "hold" && $comment_status != "0" && $comment_status != "approve" && $comment_status != "1" && $comment_status != "spam" && $comment_status != "trash"17none
!comment_ID()25–34get_comment(), comment_ID()
$comment_status != "hold" && $comment_status != "0" && !comment_ID()33–35add_action(), get_comment(), comment_ID()
!comment_ID()35–44get_comment(), comment_ID(), __(), comment_approved()
$comment_status != "hold" && $comment_status != "0" && !comment_ID()43–45add_action(), get_comment(), comment_ID(), __(), comment_approved()
comment_ID()54–63get_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–64add_action(), get_comment(), comment_ID(), clean_comment_cache(), get_comment(), do_action(), wp_transition_comment_status(), wp_update_comment_count()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev8717–648
8.58717–648
8.48717–648
8.38717–648
8.28717–648
8.18717–6481 fewer instruction than PHP 7.4
7.48817–648

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:

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

Used by · 7

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.

  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.

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.