function wp_restore_post_revision( $revision_id, $fields = null ) {}

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


  • int|object $revision_id: Revision ID or revision object.
  • array $fields: Optional. What fields to restore from. Defaults to all.

Return values

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

Defined actions

  • wp_restore_post_revision
    do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] );

Source code

function wp_restore_post_revision( $revision_id, $fields = null ) {

	if ( !$revision = wp_get_post_revision( $revision_id, ARRAY_A ) )

		return $revision;

	if ( !is_array( $fields ) )

		$fields = array_keys( _wp_post_revision_fields() );

	$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 = add_magic_quotes( $update ); //since data is from db

	$post_id = wp_update_post( $update );

	if ( is_wp_error( $post_id ) )

		return $post_id;

	if ( $post_id )

		do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] );

	return $post_id;



No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: