函数文档

wp_update_comment()

💡 云策文档标注

概述

wp_update_comment() 函数用于更新数据库中的现有评论。它通过过滤和验证输入数据,确保字段有效性,并处理评论状态转换和元数据更新。

关键要点

  • 函数接受两个参数:$commentarr(必需,包含评论信息的数组)和 $wp_error(可选,布尔值,控制失败时是否返回 WP_Error,默认为 false)。
  • 返回值:成功更新返回 1,未更新返回 0,失败时返回 false 或 WP_Error 对象。
  • 内部流程包括验证评论 ID 和文章 ID、过滤评论内容、合并新旧字段、应用钩子如 comment_save_pre 和 wp_update_comment_data,以及更新数据库和清理缓存。
  • 支持通过 $commentarr['comment_meta'] 更新评论元数据,并触发 edit_comment 动作钩子。

代码示例

$commentarr = array();
$commentarr['comment_ID'] = 123;
$commentarr['comment_approved'] = 0;
wp_update_comment( $commentarr );

注意事项

  • 使用前需确保 $commentarr 包含有效的 comment_ID,可通过 get_comment() 获取有效属性列表。
  • 评论状态值:'hold' 对应 0(待审核),'approve' 对应 1(已批准),未设置时默认为 1。
  • 从 WordPress 5.5.0 开始,无效评论或文章 ID 的返回值从 0 改为 false,以提升错误处理一致性。

📄 原文内容

Updates an existing comment in the database.

Description

Filters the comment and makes sure certain fields are valid before updating.

Parameters

$commentarrarrayrequired
Contains information on the comment.
$wp_errorbooloptional
Whether to return a WP_Error on failure.

Default:false

Return

int|false|WP_Error The value 1 if the comment was updated, 0 if not updated.
False or a WP_Error object on failure.

More Information

See get_comment() for a list of valid attributes for $commentarr

Source

function wp_update_comment( $commentarr, $wp_error = false ) {
	global $wpdb;

	// First, get all of the original fields.
	$comment = get_comment( $commentarr['comment_ID'], ARRAY_A );

	if ( empty( $comment ) ) {
		if ( $wp_error ) {
			return new WP_Error( 'invalid_comment_id', __( 'Invalid comment ID.' ) );
		} else {
			return false;
		}
	}

	// Make sure that the comment post ID is valid (if specified).
	if ( ! empty( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) {
		if ( $wp_error ) {
			return new WP_Error( 'invalid_post_id', __( 'Invalid post ID.' ) );
		} else {
			return false;
		}
	}

	$filter_comment = false;
	if ( ! has_filter( 'pre_comment_content', 'wp_filter_kses' ) ) {
		$filter_comment = ! user_can( isset( $comment['user_id'] ) ? $comment['user_id'] : 0, 'unfiltered_html' );
	}

	if ( $filter_comment ) {
		add_filter( 'pre_comment_content', 'wp_filter_kses' );
	}

	// Escape data pulled from DB.
	$comment = wp_slash( $comment );

	$old_status = $comment['comment_approved'];

	// Merge old and new fields with new fields overwriting old ones.
	$commentarr = array_merge( $comment, $commentarr );

	$commentarr = wp_filter_comment( $commentarr );

	if ( $filter_comment ) {
		remove_filter( 'pre_comment_content', 'wp_filter_kses' );
	}

	// Now extract the merged array.
	$data = wp_unslash( $commentarr );

	/**
	 * Filters the comment content before it is updated in the database.
	 *
	 * @since 1.5.0
	 *
	 * @param string $comment_content The comment data.
	 */
	$data['comment_content'] = apply_filters( 'comment_save_pre', $data['comment_content'] );

	$data['comment_date_gmt'] = get_gmt_from_date( $data['comment_date'] );

	if ( ! isset( $data['comment_approved'] ) ) {
		$data['comment_approved'] = 1;
	} elseif ( 'hold' === $data['comment_approved'] ) {
		$data['comment_approved'] = 0;
	} elseif ( 'approve' === $data['comment_approved'] ) {
		$data['comment_approved'] = 1;
	}

	$comment_id      = $data['comment_ID'];
	$comment_post_id = $data['comment_post_ID'];

	/**
	 * Filters the comment data immediately before it is updated in the database.
	 *
	 * Note: data being passed to the filter is already unslashed.
	 *
	 * @since 4.7.0
	 * @since 5.5.0 Returning a WP_Error value from the filter will short-circuit comment update
	 *              and allow skipping further processing.
	 *
	 * @param array|WP_Error $data       The new, processed comment data, or WP_Error.
	 * @param array          $comment    The old, unslashed comment data.
	 * @param array          $commentarr The new, raw comment data.
	 */
	$data = apply_filters( 'wp_update_comment_data', $data, $comment, $commentarr );

	// Do not carry on on failure.
	if ( is_wp_error( $data ) ) {
		if ( $wp_error ) {
			return $data;
		} else {
			return false;
		}
	}

	$keys = array(
		'comment_post_ID',
		'comment_author',
		'comment_author_email',
		'comment_author_url',
		'comment_author_IP',
		'comment_date',
		'comment_date_gmt',
		'comment_content',
		'comment_karma',
		'comment_approved',
		'comment_agent',
		'comment_type',
		'comment_parent',
		'user_id',
	);

	$data = wp_array_slice_assoc( $data, $keys );

	$result = $wpdb->update( $wpdb->comments, $data, array( 'comment_ID' => $comment_id ) );

	if ( false === $result ) {
		if ( $wp_error ) {
			return new WP_Error( 'db_update_error', __( 'Could not update comment in the database.' ), $wpdb->last_error );
		} else {
			return false;
		}
	}

	// If metadata is provided, store it.
	if ( isset( $commentarr['comment_meta'] ) && is_array( $commentarr['comment_meta'] ) ) {
		foreach ( $commentarr['comment_meta'] as $meta_key => $meta_value ) {
			update_comment_meta( $comment_id, $meta_key, $meta_value );
		}
	}

	clean_comment_cache( $comment_id );
	wp_update_comment_count( $comment_post_id );

	/**
	 * Fires immediately after a comment is updated in the database.
	 *
	 * The hook also fires immediately before comment status transition hooks are fired.
	 *
	 * @since 1.2.0
	 * @since 4.6.0 Added the `$data` parameter.
	 *
	 * @param int   $comment_id The comment ID.
	 * @param array $data       Comment data.
	 */
	do_action( 'edit_comment', $comment_id, $data );

	$comment = get_comment( $comment_id );

	wp_transition_comment_status( $comment->comment_approved, $old_status, $comment );

	return $result;
}

Hooks

apply_filters( ‘comment_save_pre’, string $comment_content )

Filters the comment content before it is updated in the database.

do_action( ‘edit_comment’, int $comment_id, array $data )

Fires immediately after a comment is updated in the database.

apply_filters( ‘wp_update_comment_data’, array|WP_Error $data, array $comment, array $commentarr )

Filters the comment data immediately before it is updated in the database.

Changelog

Version Description
5.5.0 The return values for an invalid comment or post ID were changed to false instead of 0.
4.9.0 Add updating comment meta during comment update.
2.0.0 Introduced.

User Contributed Notes