wp_create_post_autosave

Creates autosave data for the specified post from $_POST data.

Description

wp_create_post_autosave( (mixed) $post_data ); 

Parameters (1)

0. $post_data (mixed)
Associative array containing the post data or int post ID.

Usage

  1. if ( !function_exists( 'wp_create_post_autosave' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/post.php'; 
  3.  
  4. // Associative array containing the post data or int post ID. 
  5. $post_data = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_create_post_autosave($post_data); 
  9.  

Defined (1)

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

/wp-admin/includes/post.php  
  1. function wp_create_post_autosave( $post_data ) { 
  2. if ( is_numeric( $post_data ) ) { 
  3. $post_id = $post_data; 
  4. $post_data = $_POST; 
  5. } else { 
  6. $post_id = (int) $post_data['post_ID']; 
  7.  
  8. $post_data = _wp_translate_postdata( true, $post_data ); 
  9. if ( is_wp_error( $post_data ) ) 
  10. return $post_data; 
  11.  
  12. $post_author = get_current_user_id(); 
  13.  
  14. // Store one autosave per author. If there is already an autosave, overwrite it. 
  15. if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) { 
  16. $new_autosave = _wp_post_revision_data( $post_data, true ); 
  17. $new_autosave['ID'] = $old_autosave->ID; 
  18. $new_autosave['post_author'] = $post_author; 
  19.  
  20. // If the new autosave has the same content as the post, delete the autosave. 
  21. $post = get_post( $post_id ); 
  22. $autosave_is_different = false; 
  23. foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) { 
  24. if ( normalize_whitespace( $new_autosave[ $field ] ) != normalize_whitespace( $post->$field ) ) { 
  25. $autosave_is_different = true; 
  26. break; 
  27.  
  28. if ( ! $autosave_is_different ) { 
  29. wp_delete_post_revision( $old_autosave->ID ); 
  30. return 0; 
  31.  
  32. /** 
  33. * Fires before an autosave is stored. 
  34. * @since 4.1.0 
  35. * @param array $new_autosave Post array - the autosave that is about to be saved. 
  36. */ 
  37. do_action( 'wp_creating_autosave', $new_autosave ); 
  38.  
  39. return wp_update_post( $new_autosave ); 
  40.  
  41. // _wp_put_post_revision() expects unescaped. 
  42. $post_data = wp_unslash( $post_data ); 
  43.  
  44. // Otherwise create the new autosave as a special post revision 
  45. return _wp_put_post_revision( $post_data, true );