wp_autosave

Save a post submitted with XHR.

Description

wp_autosave( (array) $post_data ); 

Intended for use with heartbeat and autosave.js

Parameters (1)

0. $post_data (array)
Associative array of the submitted post data.

Usage

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

Defined (1)

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

/wp-admin/includes/post.php  
  1. function wp_autosave( $post_data ) { 
  2. // Back-compat 
  3. if ( ! defined( 'DOING_AUTOSAVE' ) ) 
  4. define( 'DOING_AUTOSAVE', true ); 
  5.  
  6. $post_id = (int) $post_data['post_id']; 
  7. $post_data['ID'] = $post_data['post_ID'] = $post_id; 
  8.  
  9. if ( false === wp_verify_nonce( $post_data['_wpnonce'], 'update-post_' . $post_id ) ) { 
  10. return new WP_Error( 'invalid_nonce', __( 'Error while saving.' ) ); 
  11.  
  12. $post = get_post( $post_id ); 
  13.  
  14. if ( ! current_user_can( 'edit_post', $post->ID ) ) { 
  15. return new WP_Error( 'edit_posts', __( 'Sorry, you are not allowed to edit this item.' ) ); 
  16.  
  17. if ( 'auto-draft' == $post->post_status ) 
  18. $post_data['post_status'] = 'draft'; 
  19.  
  20. if ( $post_data['post_type'] != 'page' && ! empty( $post_data['catslist'] ) ) 
  21. $post_data['post_category'] = explode( ', ', $post_data['catslist'] ); 
  22.  
  23. if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status ) ) { 
  24. // Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked 
  25. return edit_post( wp_slash( $post_data ) ); 
  26. } else { 
  27. // Non drafts or other users drafts are not overwritten. The autosave is stored in a special post revision for each user. 
  28. return wp_create_post_autosave( wp_slash( $post_data ) );