函数文档

wp_import_handle_upload()

💡 云策文档标注

概述

wp_import_handle_upload() 函数用于处理 WordPress 导入器的文件上传,并创建附件。它检查上传文件、处理上传过程,并返回上传结果或错误信息。

关键要点

  • 函数检查 $_FILES['import'] 是否存在,若不存在则返回错误信息,提示文件为空或上传配置问题。
  • 使用 wp_handle_upload() 处理文件上传,并设置覆盖选项如 test_form 和 test_type 为 false。
  • 上传成功后,构建附件数组,包括 post_title、post_content、post_mime_type 等字段,并通过 wp_insert_attachment() 保存为私有附件。
  • 安排一个一次性事件 wp_schedule_single_event(),在一天后执行清理,以防导入失败或缺少 wp_import_cleanup() 调用。
  • 返回数组包含上传文件的路径和附件 ID,或在失败时返回错误信息。

代码示例

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

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

$attachment = array(
    'post_title'     => wp_basename( $upload['file'] ),
    'post_content'   => $upload['url'],
    'post_mime_type' => $upload['type'],
    'guid'           => $upload['url'],
    'context'        => 'import',
    'post_status'    => 'private',
);

$id = wp_insert_attachment( $attachment, $upload['file'] );
wp_schedule_single_event( time() + DAY_IN_SECONDS, 'importer_scheduled_cleanup', array( $id ) );

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

注意事项

  • 函数依赖于 $_FILES['import'],确保上传表单字段名正确。
  • 上传文件会自动添加 .txt 扩展名,需注意文件类型处理。
  • 附件创建为私有状态,适用于导入场景,避免公开访问。
  • 清理事件依赖于 wp_import_cleanup(),确保导入流程完整以避免残留附件。

📄 原文内容

Handles importer uploading and adds attachment.

Return

array Uploaded file’s details on success, error message on failure.

Source

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

	$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 attachment array.
	$attachment = array(
		'post_title'     => wp_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( $attachment, $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,
	);
}

Changelog

Version Description
2.0.0 Introduced.