post_preview

Save draft or manually autosave for showing preview.

Description

post_preview(); 

Usage

  1. if ( !function_exists( 'post_preview' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/post.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = post_preview(); 
  7.  

Defined (1)

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

/wp-admin/includes/post.php  
  1. function post_preview() { 
  2.  
  3. $post_ID = (int) $_POST['post_ID']; 
  4. $_POST['ID'] = $post_ID; 
  5.  
  6. if ( ! $post = get_post( $post_ID ) ) { 
  7. wp_die( __( 'Sorry, you are not allowed to edit this post.' ) ); 
  8.  
  9. if ( ! current_user_can( 'edit_post', $post->ID ) ) { 
  10. wp_die( __( 'Sorry, you are not allowed to edit this post.' ) ); 
  11.  
  12. $is_autosave = false; 
  13.  
  14. if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'draft' == $post->post_status || 'auto-draft' == $post->post_status ) ) { 
  15. $saved_post_id = edit_post(); 
  16. } else { 
  17. $is_autosave = true; 
  18.  
  19. if ( isset( $_POST['post_status'] ) && 'auto-draft' == $_POST['post_status'] ) 
  20. $_POST['post_status'] = 'draft'; 
  21.  
  22. $saved_post_id = wp_create_post_autosave( $post->ID ); 
  23.  
  24. if ( is_wp_error( $saved_post_id ) ) 
  25. wp_die( $saved_post_id->get_error_message() ); 
  26.  
  27. $query_args = array(); 
  28.  
  29. if ( $is_autosave && $saved_post_id ) { 
  30. $query_args['preview_id'] = $post->ID; 
  31. $query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $post->ID ); 
  32.  
  33. if ( isset( $_POST['post_format'] ) ) { 
  34. $query_args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] ); 
  35.  
  36. if ( isset( $_POST['_thumbnail_id'] ) ) { 
  37. $query_args['_thumbnail_id'] = ( intval( $_POST['_thumbnail_id'] ) <= 0 ) ? '-1' : intval( $_POST['_thumbnail_id'] ); 
  38.  
  39. return get_preview_post_link( $post, $query_args );