wp_save_post_revision

Creates a revision for the current version of a post.

Description

(int|WP_Error|void) wp_save_post_revision( (int) $post_id ); 

Typically used immediately after a post update, as every update is a revision, and the most recent revision always matches the current post.

Returns (int|WP_Error|void)

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

Parameters (1)

0. $post_id (int)
The ID of the post to save as a revision.

Usage

  1. if ( !function_exists( 'wp_save_post_revision' ) ) { 
  2. require_once ABSPATH . WPINC . '/revision.php'; 
  3.  
  4. // The ID of the post to save as a revision. 
  5. $post_id = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_save_post_revision($post_id); 
  9.  

Defined (1)

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

/wp-includes/revision.php  
  1. function wp_save_post_revision( $post_id ) { 
  2. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE
  3. return; 
  4.  
  5. if ( ! $post = get_post( $post_id ) ) 
  6. return; 
  7.  
  8. if ( ! post_type_supports( $post->post_type, 'revisions' ) ) 
  9. return; 
  10.  
  11. if ( 'auto-draft' == $post->post_status ) 
  12. return; 
  13.  
  14. if ( ! wp_revisions_enabled( $post ) ) 
  15. return; 
  16.  
  17. // Compare the proposed update with the last stored revision verifying that 
  18. // they are different, unless a plugin tells us to always save regardless. 
  19. // If no previous revisions, save one 
  20. if ( $revisions = wp_get_post_revisions( $post_id ) ) { 
  21. // grab the last revision, but not an autosave 
  22. foreach ( $revisions as $revision ) { 
  23. if ( false !== strpos( $revision->post_name, "{$revision->post_parent}-revision" ) ) { 
  24. $last_revision = $revision; 
  25. break; 
  26.  
  27. /** 
  28. * Filters whether the post has changed since the last revision. 
  29. * By default a revision is saved only if one of the revisioned fields has changed. 
  30. * This filter can override that so a revision is saved even if nothing has changed. 
  31. * @since 3.6.0 
  32. * @param bool $check_for_changes Whether to check for changes before saving a new revision. 
  33. * Default true. 
  34. * @param WP_Post $last_revision The last revision post object. 
  35. * @param WP_Post $post The post object. 
  36. */ 
  37. if ( isset( $last_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', $check_for_changes = true, $last_revision, $post ) ) { 
  38. $post_has_changed = false; 
  39.  
  40. foreach ( array_keys( _wp_post_revision_fields( $post ) ) as $field ) { 
  41. if ( normalize_whitespace( $post->$field ) != normalize_whitespace( $last_revision->$field ) ) { 
  42. $post_has_changed = true; 
  43. break; 
  44.  
  45. /** 
  46. * Filters whether a post has changed. 
  47. * By default a revision is saved only if one of the revisioned fields has changed. 
  48. * This filter allows for additional checks to determine if there were changes. 
  49. * @since 4.1.0 
  50. * @param bool $post_has_changed Whether the post has changed. 
  51. * @param WP_Post $last_revision The last revision post object. 
  52. * @param WP_Post $post The post object. 
  53. */ 
  54. $post_has_changed = (bool) apply_filters( 'wp_save_post_revision_post_has_changed', $post_has_changed, $last_revision, $post ); 
  55.  
  56. //don't save revision if post unchanged 
  57. if ( ! $post_has_changed ) { 
  58. return; 
  59.  
  60. $return = _wp_put_post_revision( $post ); 
  61.  
  62. // If a limit for the number of revisions to keep has been set,  
  63. // delete the oldest ones. 
  64. $revisions_to_keep = wp_revisions_to_keep( $post ); 
  65.  
  66. if ( $revisions_to_keep < 0 ) 
  67. return $return; 
  68.  
  69. $revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) ); 
  70.  
  71. $delete = count($revisions) - $revisions_to_keep; 
  72.  
  73. if ( $delete < 1 ) 
  74. return $return; 
  75.  
  76. $revisions = array_slice( $revisions, 0, $delete ); 
  77.  
  78. for ( $i = 0; isset( $revisions[$i] ); $i++ ) { 
  79. if ( false !== strpos( $revisions[ $i ]->post_name, 'autosave' ) ) 
  80. continue; 
  81.  
  82. wp_delete_post_revision( $revisions[ $i ]->ID ); 
  83.  
  84. return $return;