get_default_post_to_edit

Default post information to use when populating the "Write Post" form.

Description

(WP_Post) get_default_post_to_edit( (string) $post_type = 'post', (bool) $create_in_db = false ); 

Returns (WP_Post)

Post object containing all the default post data as attributes

Parameters (2)

0. $post_type — Optional. (string) => 'post'
A post type string. Default post..
1. $create_in_db — Optional. (bool) => false
Whether to insert the post into database. Default false.

Usage

  1. if ( !function_exists( 'get_default_post_to_edit' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/post.php'; 
  3.  
  4. // Optional. A post type string. Default 'post'. 
  5. $post_type = 'post'; 
  6.  
  7. // Optional. Whether to insert the post into database. Default false. 
  8. $create_in_db = false; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_default_post_to_edit($post_type, $create_in_db); 
  12.  

Defined (1)

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

/wp-admin/includes/post.php  
  1. function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) { 
  2. $post_title = ''; 
  3. if ( !empty( $_REQUEST['post_title'] ) ) 
  4. $post_title = esc_html( wp_unslash( $_REQUEST['post_title'] )); 
  5.  
  6. $post_content = ''; 
  7. if ( !empty( $_REQUEST['content'] ) ) 
  8. $post_content = esc_html( wp_unslash( $_REQUEST['content'] )); 
  9.  
  10. $post_excerpt = ''; 
  11. if ( !empty( $_REQUEST['excerpt'] ) ) 
  12. $post_excerpt = esc_html( wp_unslash( $_REQUEST['excerpt'] )); 
  13.  
  14. if ( $create_in_db ) { 
  15. $post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) ); 
  16. $post = get_post( $post_id ); 
  17. if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) ) 
  18. set_post_format( $post, get_option( 'default_post_format' ) ); 
  19. } else { 
  20. $post = new stdClass; 
  21. $post->ID = 0; 
  22. $post->post_author = ''; 
  23. $post->post_date = ''; 
  24. $post->post_date_gmt = ''; 
  25. $post->post_password = ''; 
  26. $post->post_name = ''; 
  27. $post->post_type = $post_type; 
  28. $post->post_status = 'draft'; 
  29. $post->to_ping = ''; 
  30. $post->pinged = ''; 
  31. $post->comment_status = get_default_comment_status( $post_type ); 
  32. $post->ping_status = get_default_comment_status( $post_type, 'pingback' ); 
  33. $post->post_pingback = get_option( 'default_pingback_flag' ); 
  34. $post->post_category = get_option( 'default_category' ); 
  35. $post->page_template = 'default'; 
  36. $post->post_parent = 0; 
  37. $post->menu_order = 0; 
  38. $post = new WP_Post( $post ); 
  39.  
  40. /** 
  41. * Filters the default post content initially used in the "Write Post" form. 
  42. * @since 1.5.0 
  43. * @param string $post_content Default post content. 
  44. * @param WP_Post $post Post object. 
  45. */ 
  46. $post->post_content = apply_filters( 'default_content', $post_content, $post ); 
  47.  
  48. /** 
  49. * Filters the default post title initially used in the "Write Post" form. 
  50. * @since 1.5.0 
  51. * @param string $post_title Default post title. 
  52. * @param WP_Post $post Post object. 
  53. */ 
  54. $post->post_title = apply_filters( 'default_title', $post_title, $post ); 
  55.  
  56. /** 
  57. * Filters the default post excerpt initially used in the "Write Post" form. 
  58. * @since 1.5.0 
  59. * @param string $post_excerpt Default post excerpt. 
  60. * @param WP_Post $post Post object. 
  61. */ 
  62. $post->post_excerpt = apply_filters( 'default_excerpt', $post_excerpt, $post ); 
  63.  
  64. return $post;