函数文档

get_default_post_to_edit()

💡 云策文档标注

概述

get_default_post_to_edit() 函数用于返回默认的文章信息,以填充“撰写文章”表单。它根据参数生成一个 WP_Post 对象,包含标题、内容、摘要等默认属性,并可选择是否将文章插入数据库。

关键要点

  • 参数:$post_type(可选,默认 'post')指定文章类型;$create_in_db(可选,默认 false)控制是否将文章插入数据库。
  • 返回值:返回一个 WP_Post 对象,包含默认文章数据作为属性。
  • 功能:如果 $create_in_db 为 true,则插入一个状态为 'auto-draft' 的文章到数据库,并设置默认文章格式;否则,创建一个临时的 WP_Post 对象。
  • 过滤器:使用 default_content、default_title 和 default_excerpt 过滤器允许开发者自定义默认内容、标题和摘要。
  • 注意事项:函数会从 $_REQUEST 中提取 post_title、content 和 excerpt 值,这可能影响预期行为;返回的 post_category 可能为标量,需小心处理以避免警告。

代码示例

function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) {
    // 函数实现代码(略)
    return $post;
}

📄 原文内容

Returns default post information to use when populating the “Write Post” form.

Parameters

$post_typestringoptional
A post type string. Default 'post'.
$create_in_dbbooloptional
Whether to insert the post into database.

Default:false

Return

WP_Post Post object containing all the default post data as attributes

Source

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;
}

Hooks

apply_filters( ‘default_content’, string $post_content, WP_Post $post )

Filters the default post content initially used in the “Write Post” form.

apply_filters( ‘default_excerpt’, string $post_excerpt, WP_Post $post )

Filters the default post excerpt initially used in the “Write Post” form.

apply_filters( ‘default_title’, string $post_title, WP_Post $post )

Filters the default post title initially used in the “Write Post” form.

Changelog

Version Description
2.0.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Before using this function for your own theme or plugin, be wary of these non-obvious implicit behaviors (as of 4.2.3):

    • The returned post will have a post_category member set, even if the $post_type does not use the category taxonomy.
    • This post_category value will be scalar, which causes warnings passed into wp_insert_post without additional manipulation.
    • Some of the post’s fields will be pulled out of $_REQUEST if present, which may not be the intended goal.