返回介绍

wp_import_handle_upload()

发布于 2017-09-11 12:12:39 字数 2153 浏览 1034 评论 0 收藏 0

wp_import_handle_upload()

Handle importer uploading and add attachment.


description


返回值

(array) Uploaded file's details on success, error message on failure


源代码

File: wp-admin/includes/import.php

function wp_import_handle_upload() {
	if ( ! isset( $_FILES['import'] ) ) {
		return array(
			'error' => __( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.' )
		);
	}

	$overrides = array( 'test_form' => false, 'test_type' => false );
	$_FILES['import']['name'] .= '.txt';
	$upload = wp_handle_upload( $_FILES['import'], $overrides );

	if ( isset( $upload['error'] ) ) {
		return $upload;
	}

	// Construct the object array
	$object = array(
		'post_title' => basename( $upload['file'] ),
		'post_content' => $upload['url'],
		'post_mime_type' => $upload['type'],
		'guid' => $upload['url'],
		'context' => 'import',
		'post_status' => 'private'
	);

	// Save the data
	$id = wp_insert_attachment( $object, $upload['file'] );

	/*
	 * Schedule a cleanup for one day from now in case of failed
	 * import or missing wp_import_cleanup() call.
	 */
	wp_schedule_single_event( time() + DAY_IN_SECONDS, 'importer_scheduled_cleanup', array( $id ) );

	return array( 'file' => $upload['file'], 'id' => $id );
}

更新日志

Versiondescription
2.0.0Introduced.

相关函数

Uses

  • wp-admin/includes/file.php: wp_handle_upload()
  • wp-includes/cron.php: wp_schedule_single_event()
  • wp-includes/l10n.php: __()
  • wp-includes/post.php: wp_insert_attachment()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文