wp_restore_post_revision

Restores a post to the specified revision.

Description

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

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

Returns (int|false|null)

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

Parameters (2)

0. $revision_id (int|WP_Post)
Revision ID or revision object.
1. $fields — Optional. (null) => null
What fields to restore from. Defaults to all.

Usage

  1. if ( !function_exists( 'wp_restore_post_revision' ) ) { 
  2. require_once ABSPATH . WPINC . '/revision.php'; 
  3.  
  4. // Revision ID or revision object. 
  5. $revision_id = null; 
  6.  
  7. // Optional. What fields to restore from. Defaults to all. 
  8. $fields = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_restore_post_revision($revision_id, $fields); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/revision.php  
  1. function wp_restore_post_revision( $revision_id, $fields = null ) { 
  2. if ( !$revision = wp_get_post_revision( $revision_id, ARRAY_A ) ) 
  3. return $revision; 
  4.  
  5. if ( !is_array( $fields ) ) 
  6. $fields = array_keys( _wp_post_revision_fields( $revision ) ); 
  7.  
  8. $update = array(); 
  9. foreach ( array_intersect( array_keys( $revision ), $fields ) as $field ) { 
  10. $update[$field] = $revision[$field]; 
  11.  
  12. if ( !$update ) 
  13. return false; 
  14.  
  15. $update['ID'] = $revision['post_parent']; 
  16.  
  17. $update = wp_slash( $update ); //since data is from db 
  18.  
  19. $post_id = wp_update_post( $update ); 
  20. if ( ! $post_id || is_wp_error( $post_id ) ) 
  21. return $post_id; 
  22.  
  23. // Update last edit user 
  24. update_post_meta( $post_id, '_edit_last', get_current_user_id() ); 
  25.  
  26. /** 
  27. * Fires after a post revision has been restored. 
  28. * @since 2.6.0 
  29. * @param int $post_id Post ID. 
  30. * @param int $revision_id Post revision ID. 
  31. */ 
  32. do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] ); 
  33.  
  34. return $post_id;