get_default_post_to_edit()WP 2.0.0

Gets default data for the post form when adding a new post in the admin panel.

When we click to add a post in the admin panel, a post is created in the database with the status draft. This function is responsible for what data will be created and what data we will see in the post addition form.

To use the function on the front end, you need to include the file:

require_once ABSPATH . 'wp-admin/includes/post.php';

The function has three hooks that allow you to change: the title, content, and excerpt (quote) of the newly created post.

$post->post_content = apply_filters( 'default_content', $post_content, $post );

$post->post_title   = apply_filters( 'default_title',   $post_title,   $post );

$post->post_excerpt = apply_filters( 'default_excerpt', $post_excerpt, $post );

Returns

WP_Post. A post object that contains all the default data of the post.

Usage

get_default_post_to_edit( $post_type, $create_in_db );
$post_type(string)
The type of post for which the data will be created.
Default: 'post'
$create_in_db(true/false)
Whether to insert (add) data into the database. If set to false, the function will simply collect the post object and return it, without creating anything in the database.
Default: false

Examples

0

#1 Example of creating a new post data

require_once ABSPATH . 'wp-admin/includes/post.php';

$def_postdata = get_default_post_to_edit( 'post', false );

print_r( $def_postdata );

/*
WP_Post Object
(
	[ID] => 0
	[post_author] => 
	[post_date] => 
	[post_date_gmt] => 
	[post_content] => 
	[post_title] => 
	[post_excerpt] => 
	[post_status] => draft
	[comment_status] => open
	[ping_status] => open
	[post_password] => 
	[post_name] => 
	[to_ping] => 
	[pinged] => 
	[post_modified] => 0000-00-00 00:00:00
	[post_modified_gmt] => 0000-00-00 00:00:00
	[post_content_filtered] => 
	[post_parent] => 0
	[guid] => 
	[menu_order] => 0
	[post_type] => post
	[post_mime_type] => 
	[comment_count] => 0
	[filter] => 
	[post_pingback] => 
	[post_category] => 1
	[page_template] => default
)
*/

Changelog

Since 2.0.0 Introduced.

get_default_post_to_edit() code WP 6.9.1

function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) {
	$post_title = '';
	if ( ! empty( $_REQUEST['post_title'] ) ) {
		$post_title = esc_html( wp_unslash( $_REQUEST['post_title'] ) );
	}

	$post_content = '';
	if ( ! empty( $_REQUEST['content'] ) ) {
		$post_content = esc_html( wp_unslash( $_REQUEST['content'] ) );
	}

	$post_excerpt = '';
	if ( ! empty( $_REQUEST['excerpt'] ) ) {
		$post_excerpt = esc_html( wp_unslash( $_REQUEST['excerpt'] ) );
	}

	if ( $create_in_db ) {
		$post_id = wp_insert_post(
			array(
				'post_title'  => __( 'Auto Draft' ),
				'post_type'   => $post_type,
				'post_status' => 'auto-draft',
			),
			true,
			false
		);

		if ( is_wp_error( $post_id ) ) {
			wp_die( $post_id->get_error_message() );
		}

		$post = get_post( $post_id );

		if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) ) {
			set_post_format( $post, get_option( 'default_post_format' ) );
		}

		wp_after_insert_post( $post, false, null );

		// Schedule auto-draft cleanup.
		if ( ! wp_next_scheduled( 'wp_scheduled_auto_draft_delete' ) ) {
			wp_schedule_event( time(), 'daily', 'wp_scheduled_auto_draft_delete' );
		}
	} else {
		$post                 = new stdClass();
		$post->ID             = 0;
		$post->post_author    = '';
		$post->post_date      = '';
		$post->post_date_gmt  = '';
		$post->post_password  = '';
		$post->post_name      = '';
		$post->post_type      = $post_type;
		$post->post_status    = 'draft';
		$post->to_ping        = '';
		$post->pinged         = '';
		$post->comment_status = get_default_comment_status( $post_type );
		$post->ping_status    = get_default_comment_status( $post_type, 'pingback' );
		$post->post_pingback  = get_option( 'default_pingback_flag' );
		$post->post_category  = get_option( 'default_category' );
		$post->page_template  = 'default';
		$post->post_parent    = 0;
		$post->menu_order     = 0;
		$post                 = new WP_Post( $post );
	}

	/**
	 * Filters the default post content initially used in the "Write Post" form.
	 *
	 * @since 1.5.0
	 *
	 * @param string  $post_content Default post content.
	 * @param WP_Post $post         Post object.
	 */
	$post->post_content = (string) apply_filters( 'default_content', $post_content, $post );

	/**
	 * Filters the default post title initially used in the "Write Post" form.
	 *
	 * @since 1.5.0
	 *
	 * @param string  $post_title Default post title.
	 * @param WP_Post $post       Post object.
	 */
	$post->post_title = (string) apply_filters( 'default_title', $post_title, $post );

	/**
	 * Filters the default post excerpt initially used in the "Write Post" form.
	 *
	 * @since 1.5.0
	 *
	 * @param string  $post_excerpt Default post excerpt.
	 * @param WP_Post $post         Post object.
	 */
	$post->post_excerpt = (string) apply_filters( 'default_excerpt', $post_excerpt, $post );

	return $post;
}