wp_set_comment_status()
wp_set_comment_status( int|WP_Comment $comment_id, stri…
wp_set_comment_status( int|WP_Comment $comment_id, string $comment_status, bool $wp_error = false )
设置注释的状态。
Sets the status of a comment.
目录锚点:#说明#参数#源码
说明(Description)
“wp_set_comment_status”操作在处理注释后调用。如果注释状态不在列表中,则返回false。
参数(Parameters)
| 参数 | 类型 | 说明 |
|---|---|---|
| $comment_id | (int | WP_Comment) | 注释ID或WP_注释对象。 |
| $comment_status | (string) | 新的评论状态,可以是“保留”、“批准”、“垃圾邮件”或“垃圾箱”。 |
| $wp_error | (bool) | 如果出现故障,是否返回WP_Error对象。默认值为false。 |
源码(Source)
/**
* Sets the status of a comment.
*
* 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.
*
* @since 1.0.0
*
* global wpdb $wpdb
*
* @param int $comment_id Comment ID.
* @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
* @param bool $wp_error Whether to return a WP_Error object if there is a failure. Default is false.
* @return bool|WP_Error True on success, false or WP_Error on failure.
*/
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';
if ( get_option('comments_notify') ) {
wp_notify_postauthor( $comment_id );
}
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_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_id);
$comment = get_comment($comment_id);
/**
* Fires immediately before transitioning a comment's status from one to another
* in the database.
*
* @since 1.5.0
*
* @param int $comment_id Comment ID.
* @param string|bool $comment_status Current comment status. Possible values include
* 'hold', 'approve', 'spam', 'trash', or false.
*/
do_action( 'wp_set_comment_status', $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;
}| 更新版本 | 源码位置 | 使用 | 被使用 |
|---|---|---|---|
| 1.0.0 | wp-includes/comment.php | 11 | 9 |
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

还没有任何评论,赶紧来占个楼吧!