函数文档

wp_write_post()

💡 云策文档标注

概述

wp_write_post() 函数用于处理“撰写文章”表单提交,基于 $_POST 数据创建新文章。它执行权限检查、数据处理,并调用 wp_insert_post() 完成创建,返回文章ID或WP_Error。

关键要点

  • 函数基于 $_POST 数据创建新文章,若存在 post_ID 则调用 edit_post() 进行编辑
  • 执行用户权限验证,使用 current_user_can() 检查编辑权限,失败时返回 WP_Error
  • 处理文章可见性设置(如公开、密码保护、私有),调整相关 $_POST 字段
  • 调用 _wp_translate_postdata() 和 _wp_get_allowed_postdata() 进行数据转换和过滤
  • 通过 wp_insert_post() 插入文章,并添加元数据、设置编辑锁等后续操作
  • 返回类型为 int|WP_Error,成功时返回文章ID,失败时返回 WP_Error

代码示例

function wp_write_post() {
    if ( isset( $_POST['post_type'] ) ) {
        $ptype = get_post_type_object( $_POST['post_type'] );
    } else {
        $ptype = get_post_type_object( 'post' );
    }

    if ( ! current_user_can( $ptype->cap->edit_posts ) ) {
        if ( 'page' === $ptype->name ) {
            return new WP_Error( 'edit_pages', __( 'Sorry, you are not allowed to create pages on this site.' ) );
        } else {
            return new WP_Error( 'edit_posts', __( 'Sorry, you are not allowed to create posts or drafts on this site.' ) );
        }
    }

    $_POST['post_mime_type'] = '';
    unset( $_POST['filter'] );

    if ( isset( $_POST['post_ID'] ) ) {
        return edit_post();
    }

    if ( isset( $_POST['visibility'] ) ) {
        switch ( $_POST['visibility'] ) {
            case 'public':
                $_POST['post_password'] = '';
                break;
            case 'password':
                unset( $_POST['sticky'] );
                break;
            case 'private':
                $_POST['post_status']   = 'private';
                $_POST['post_password'] = '';
                unset( $_POST['sticky'] );
                break;
        }
    }

    $translated = _wp_translate_postdata( false );
    if ( is_wp_error( $translated ) ) {
        return $translated;
    }
    $translated = _wp_get_allowed_postdata( $translated );

    $post_id = wp_insert_post( $translated );
    if ( is_wp_error( $post_id ) ) {
        return $post_id;
    }

    if ( empty( $post_id ) ) {
        return 0;
    }

    add_meta( $post_id );
    add_post_meta( $post_id, '_edit_last', $GLOBALS['current_user']->ID );
    _fix_attachment_links( $post_id );
    wp_set_post_lock( $post_id );

    return $post_id;
}

注意事项

  • 函数依赖于 $_POST 全局变量,确保表单数据正确提交
  • 权限检查基于文章类型能力,需配置正确的用户角色和权限
  • 错误处理通过 WP_Error 对象,调用时应检查返回值类型
  • 函数内部调用多个辅助函数(如 _wp_translate_postdata),需了解其数据转换逻辑

📄 原文内容

Creates a new post from the “Write Post” form using $_POST information.

Return

int|WP_Error Post ID on success, WP_Error on failure.

Source

function wp_write_post() {
	if ( isset( $_POST['post_type'] ) ) {
		$ptype = get_post_type_object( $_POST['post_type'] );
	} else {
		$ptype = get_post_type_object( 'post' );
	}

	if ( ! current_user_can( $ptype->cap->edit_posts ) ) {
		if ( 'page' === $ptype->name ) {
			return new WP_Error( 'edit_pages', __( 'Sorry, you are not allowed to create pages on this site.' ) );
		} else {
			return new WP_Error( 'edit_posts', __( 'Sorry, you are not allowed to create posts or drafts on this site.' ) );
		}
	}

	$_POST['post_mime_type'] = '';

	// Clear out any data in internal vars.
	unset( $_POST['filter'] );

	// Edit, don't write, if we have a post ID.
	if ( isset( $_POST['post_ID'] ) ) {
		return edit_post();
	}

	if ( isset( $_POST['visibility'] ) ) {
		switch ( $_POST['visibility'] ) {
			case 'public':
				$_POST['post_password'] = '';
				break;
			case 'password':
				unset( $_POST['sticky'] );
				break;
			case 'private':
				$_POST['post_status']   = 'private';
				$_POST['post_password'] = '';
				unset( $_POST['sticky'] );
				break;
		}
	}

	$translated = _wp_translate_postdata( false );
	if ( is_wp_error( $translated ) ) {
		return $translated;
	}
	$translated = _wp_get_allowed_postdata( $translated );

	// Create the post.
	$post_id = wp_insert_post( $translated );
	if ( is_wp_error( $post_id ) ) {
		return $post_id;
	}

	if ( empty( $post_id ) ) {
		return 0;
	}

	add_meta( $post_id );

	add_post_meta( $post_id, '_edit_last', $GLOBALS['current_user']->ID );

	// Now that we have an ID we can fix any attachment anchor hrefs.
	_fix_attachment_links( $post_id );

	wp_set_post_lock( $post_id );

	return $post_id;
}

Changelog

Version Description
2.1.0 Introduced.