wp_restore_post_revision

Advertisement

Summery Summery

Restores a post to the specified revision.

Syntax Syntax

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

Description Description

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

Parameters 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

Return Return

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

Source Source

File: wp-includes/revision.php

	return $revision;
}

/**
 * Restores a post to the specified revision.
 *
 * Can restore a past revision using all fields of the post revision, or only selected fields.
 *
 * @since 2.6.0
 *
 * @param int|WP_Post $revision_id Revision ID or revision object.
 * @param array       $fields      Optional. What fields to restore from. Defaults to all.
 * @return int|false|null Null if error, false if no fields to restore, (int) post ID if success.
 */
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;
	}

Advertisement

Changelog Changelog

Changelog
Version Description
2.6.0 Introduced.

Advertisement

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.