wp_trash_comment() │ WP 2.9.0

Moves a comment to the Trash.

Stores the comment's previous status and the time it was trashed. This data is used when restoring it with wp_untrash_comment().

If EMPTY_TRASH_DAYS = 0, control is passed to wp_delete_comment().

When processing a top-level comment of the note type, the function also trashes or deletes its direct child comments of the note type.

The function does not check user capabilities or the nonce. When called from a user request, perform these checks separately.

Processing child notes is not a transaction. The function can return false when the parent note has already been trashed but processing one of the child notes failed.

Use wp_untrash_comment() to restore a comment.

Use wp_delete_comment() to delete a comment.

Hooks from the function

Returns

bool.

  • true - the comment and related child notes were successfully trashed or deleted.
  • false - the comment was not found, its status could not be changed or it could not be deleted, or one of the child notes could not be processed.

Usage

wp_trash_comment( $comment_id );
$comment_id(int|WP_Comment) (required)
Comment ID or comment object.

Examples

#1 Move a comment to the Trash

Check the current user's capabilities and move the comment to the Trash:

$comment_id = 123;

if ( current_user_can( 'edit_comment', $comment_id ) ) {
	$result = wp_trash_comment( $comment_id );

	if ( ! $result ) {
		wp_die( 'Could not move the comment to the Trash.' );
	}
}

Changelog

Since 2.9.0 Introduced.
Since 6.9.0 Any child notes are deleted when deleting a note.

wp_trash_comment() code WP 7.1.2

function wp_trash_comment( $comment_id ) {
	if ( ! EMPTY_TRASH_DAYS ) {
		$comment = get_comment( $comment_id );
		$success = wp_delete_comment( $comment_id, true );

		if ( ! $success ) {
			return false;
		}

		// Also delete children of top level 'note' type comments.
		if ( $comment && 'note' === $comment->comment_type && 0 === (int) $comment->comment_parent ) {
			$children = $comment->get_children(
				array(
					'fields' => 'ids',
					'status' => 'all',
					'type'   => 'note',
				)
			);

			foreach ( $children as $child_id ) {
				if ( ! wp_delete_comment( $child_id, true ) ) {
					$success = false;
				}
			}
		}

		return $success;
	}

	$comment = get_comment( $comment_id );
	if ( ! $comment ) {
		return false;
	}

	/**
	 * Fires immediately before a comment is sent to the Trash.
	 *
	 * @since 2.9.0
	 * @since 4.9.0 Added the `$comment` parameter.
	 *
	 * @param string     $comment_id The comment ID as a numeric string.
	 * @param WP_Comment $comment    The comment to be trashed.
	 */
	do_action( 'trash_comment', $comment->comment_ID, $comment );

	if ( wp_set_comment_status( $comment, 'trash' ) ) {
		delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
		delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
		add_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', $comment->comment_approved );
		add_comment_meta( $comment->comment_ID, '_wp_trash_meta_time', time() );

		/**
		 * Fires immediately after a comment is sent to Trash.
		 *
		 * @since 2.9.0
		 * @since 4.9.0 Added the `$comment` parameter.
		 *
		 * @param string     $comment_id The comment ID as a numeric string.
		 * @param WP_Comment $comment    The trashed comment.
		 */
		do_action( 'trashed_comment', $comment->comment_ID, $comment );

		// For top level 'note' type comments, also trash children.
		if ( 'note' === $comment->comment_type && 0 === (int) $comment->comment_parent ) {
			$children = $comment->get_children(
				array(
					'fields' => 'ids',
					'status' => 'all',
					'type'   => 'note',
				)
			);

			$success = true;
			foreach ( $children as $child_id ) {
				if ( ! wp_trash_comment( $child_id ) ) {
					$success = false;
				}
			}
			return $success;
		}

		return true;
	}

	return false;
}