wp_set_comment_status

函数
wp_set_comment_status ( $comment_id, $comment_status, $wp_error = false )
参数
  • (int|WP_Comment) $comment_id Comment ID or WP_Comment object.
    Required:
  • (string) $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
    Required:
  • (bool) $wp_error Whether to return a WP_Error object if there is a failure. Default false.
    Required:
    Default: false
返回值
  • (bool|WP_Error) True on success, false or WP_Error on failure.
定义位置
相关方法
wp_get_comment_statusget_comment_statuseswp_transition_comment_statuswp_list_commentswp_set_comment_cookies
引入
1.0.0
弃用
-

wp_set_comment_status: 这是一个WordPress的函数,用来设置评论的状态。它允许你将评论的状态设置为”批准”、”垃圾邮件”、”垃圾”或”未批准”。

设置一个评论的状态。

{@see ‘wp_set_comment_status’}动作在处理完评论后被调用。如果评论状态不在列表中,那么就会返回false。

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;
}

常见问题

FAQs
查看更多 >