wppaste
WordPress

wp_update_comment( array $commentarr, bool $wp_error = false ): int|false|WP_Error

Since
2.0.0, 4.9.0, 5.5.0, 5.5.0
Source
wp-includes/comment.php:2869
Updates an existing comment in the database.

Description

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

Parameters

$commentarrarray
Contains information on the comment.
$wp_errorbooloptional
Whether to return a WP_Error on failure. Default false.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.

Hooks fired · 3

3 hooks fire while wp_update_comment() runs, in this order:

  1. apply_filters( comment_save_pre )filterline 2925 (+56 into the body)

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

  2. apply_filters( wp_update_comment_data )filterline 2953 (+84 into the body)

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

  3. do_action( edit_comment )actionline 3014 (+145 into the body)

    Fires immediately after a comment is updated in the database.

Uses · 20

  • get_comment()Retrieves comment data given a comment ID or comment object.
  • __()Retrieves the translation of $text.
  • get_post()Retrieves post data given a post ID or post object.
  • has_filter()Checks if any filter has been registered for a hook.
  • user_can()Returns whether a particular user has the specified capability.
  • add_filter()Adds a callback function to a filter hook.
  • wp_slash()Adds slashes to a string or recursively adds slashes to strings within an array.
  • wp_filter_comment()Filters and sanitizes comment data.
  • remove_filter()Removes a callback function from a filter hook.
  • wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • get_gmt_from_date()Given a date in the timezone of the site, returns that date in UTC.
Show all 20

Used by · 3

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( $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.	 *

History

Introduced in 2.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

5.5.0
The return values for an invalid comment or post ID were changed to false instead of 0.from the docblock
5.5.0
The $wp_error parameter was added.from the docblock
4.9.0
Add updating comment meta during comment update.from the docblock
2.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/comment.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.