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:2509
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:
- apply_filters( comment_save_pre )filterline 2565 (+56 into the body)
Filters the comment content before it is updated in the database.
- apply_filters( wp_update_comment_data )filterline 2593 (+84 into the body)
Filters the comment data immediately before it is updated in the database.
- do_action( edit_comment )actionline 2654 (+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
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_array_slice_assoc()Extracts a slice of an array, given a list of keys.
- update_comment_meta()Updates comment meta field based on comment ID.
- clean_comment_cache()Removes a comment from the object cache.
- wp_update_comment_count()Updates the comment count for post(s).
- do_action()Calls the callback functions that have been added to an action hook.
- wp_transition_comment_status()Calls hooks for when a comment status transition occurs.
- WP_Error::__construct()Initializes the error.
Used by · 3
- WP_REST_Comments_Controller::update_item()Updates a comment.
- edit_comment()Updates a comment with values provided in $_POST.
- wp_xmlrpc_server::wp_editComment()Edits a comment.
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. *History
Introduced in 2.0.0. Unchanged from 6.7.7 through 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 docblock4.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 6.7.7 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.