wp_set_comment_status()
云策文档标注
概述
wp_set_comment_status() 函数用于设置评论的状态,支持 'hold'、'approve'、'spam' 或 'trash' 等状态。函数在状态更新后触发相关 Hook,并返回操作结果。
关键要点
- 函数参数包括 $comment_id(评论 ID 或 WP_Comment 对象)、$comment_status(新状态)和可选的 $wp_error(是否返回 WP_Error)。
- 状态值映射:'hold' 或 '0' 对应 '0','approve' 或 '1' 对应 '1','spam' 对应 'spam','trash' 对应 'trash',其他值返回 false。
- 更新数据库后,会清理评论缓存、触发 wp_set_comment_status 动作、调用 wp_transition_comment_status 和更新评论计数。
- 返回类型为 bool 或 WP_Error,成功时返回 true,失败时根据 $wp_error 参数返回 false 或 WP_Error。
代码示例
// 设置评论 ID 为 123 的状态为 'approve'
$result = wp_set_comment_status( 123, 'approve' );
if ( $result ) {
echo '评论状态更新成功';
} else {
echo '更新失败';
}注意事项
- 确保 $comment_status 参数为有效值,否则函数可能返回 false。
- 使用 $wp_error 参数可获取更详细的错误信息,便于调试。
- 函数内部会触发 wp_set_comment_status 动作,开发者可挂接自定义逻辑。
原文内容
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.
Parameters
$comment_idint|WP_Commentrequired-
Comment ID or WP_Comment object.
$comment_statusstringrequired-
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
Source
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;
}
Hooks
- do_action( ‘wp_set_comment_status’, string $comment_id, string $comment_status )
-
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.
Changelog
| Version | Description |
|---|---|
| 1.0.0 | Introduced. |
Skip to note 2 content
sinanisler
this is related to the content.
how do we get comment status for a post;
comment_status; // get comment status if ( $status == 'open' ) { // do something only when comments are open } ?>