_wp_put_post_revision

Inserts post data into the posts table as a post revision.

Description

(int|WP_Error) _wp_put_post_revision( (constant) $post = null, (bool) $autosave = false ); 

Returns (int|WP_Error)

WP_Error or 0 if error, new revision ID if success.

Parameters (2)

0. $post — Optional. (constant) => null
Post ID, post object OR post array.
1. $autosave — Optional. (bool) => false
Is the revision an autosave?

Usage

  1. if ( !function_exists( '_wp_put_post_revision' ) ) { 
  2. require_once ABSPATH . WPINC . '/revision.php'; 
  3.  
  4. // Post ID, post object OR post array. 
  5. $post = null; 
  6.  
  7. // Optional. Is the revision an autosave? 
  8. $autosave = false; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = _wp_put_post_revision($post, $autosave); 
  12.  

Defined (1)

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

/wp-includes/revision.php  
  1. function _wp_put_post_revision( $post = null, $autosave = false ) { 
  2. if ( is_object($post) ) 
  3. $post = get_object_vars( $post ); 
  4. elseif ( !is_array($post) ) 
  5. $post = get_post($post, ARRAY_A); 
  6.  
  7. if ( ! $post || empty($post['ID']) ) 
  8. return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); 
  9.  
  10. if ( isset($post['post_type']) && 'revision' == $post['post_type'] ) 
  11. return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) ); 
  12.  
  13. $post = _wp_post_revision_data( $post, $autosave ); 
  14. $post = wp_slash($post); //since data is from db 
  15.  
  16. $revision_id = wp_insert_post( $post ); 
  17. if ( is_wp_error($revision_id) ) 
  18. return $revision_id; 
  19.  
  20. if ( $revision_id ) { 
  21. /** 
  22. * Fires once a revision has been saved. 
  23. * @since 2.6.0 
  24. * @param int $revision_id Post revision ID. 
  25. */ 
  26. do_action( '_wp_put_post_revision', $revision_id ); 
  27.  
  28. return $revision_id;