wp_restore_post_revision() WordPress Function

The wp_restore_post_revision() function restores a post revision. This function takes two parameters: the revision ID and the post ID. The revision ID is the ID of the revision to be restored, and the post ID is the ID of the post that the revision belongs to.

wp_restore_post_revision( int|WP_Post $revision_id, array $fields = null ) #

Restores a post to the specified revision.


Description

Can restore a past revision using all fields of the post revision, or only selected fields.


Top ↑

Parameters

$revision_id

(int|WP_Post)(Required)Revision ID or revision object.

$fields

(array)(Optional) What fields to restore from. Defaults to all.

Default value: null


Top ↑

Return

(int|false|null) Null if error, false if no fields to restore, (int) post ID if success.


Top ↑

Source

File: wp-includes/revision.php

function wp_restore_post_revision( $revision_id, $fields = null ) {
	$revision = wp_get_post_revision( $revision_id, ARRAY_A );
	if ( ! $revision ) {
		return $revision;
	}

	if ( ! is_array( $fields ) ) {
		$fields = array_keys( _wp_post_revision_fields( $revision ) );
	}

	$update = array();
	foreach ( array_intersect( array_keys( $revision ), $fields ) as $field ) {
		$update[ $field ] = $revision[ $field ];
	}

	if ( ! $update ) {
		return false;
	}

	$update['ID'] = $revision['post_parent'];

	$update = wp_slash( $update ); // Since data is from DB.

	$post_id = wp_update_post( $update );
	if ( ! $post_id || is_wp_error( $post_id ) ) {
		return $post_id;
	}

	// Update last edit user.
	update_post_meta( $post_id, '_edit_last', get_current_user_id() );

	/**
	 * Fires after a post revision has been restored.
	 *
	 * @since 2.6.0
	 *
	 * @param int $post_id     Post ID.
	 * @param int $revision_id Post revision ID.
	 */
	do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] );

	return $post_id;
}


Top ↑

Changelog

Changelog
VersionDescription
2.6.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.