wp_update_comment( array $commentarr )

Updates an existing comment in the database.


Description

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


Parameters

$commentarr

(Required) Contains information on the comment.


Return

(int) Comment was updated if value is 1, or was not updated if value is 0.


Source

File: wp-includes/comment.php

function wp_update_comment($commentarr) {
	global $wpdb;

	// First, get all of the original fields
	$comment = get_comment($commentarr['comment_ID'], ARRAY_A);
	if ( empty( $comment ) ) {
		return 0;
	}

	// Make sure that the comment post ID is valid (if specified).
	if ( ! empty( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) {
		return 0;
	}

	// 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 );

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

	/**
	 * Filters the comment content before it is updated in the database.
	 *
	 * @since WP-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 WP-4.7.0
	 *
	 * @param array $data       The new, processed comment data.
	 * @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 );

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

	$rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) );

	// 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 WP-1.2.0
	 * @since WP-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 $rval;
}


Changelog

Changelog
Version Description
WP-4.9.0 Add updating comment meta during comment update.
WP-2.0.0 Introduced.