wp_import_upload_form()
概述
wp_import_upload_form() 函数用于输出导入器接受导入数据的表单。它处理上传目录错误、文件大小限制和表单生成。
关键要点
- 输出导入数据上传表单,包含文件选择和提交按钮。
- 使用 import_upload_size_limit 过滤器调整最大上传文件大小,默认基于 wp_max_upload_size()。
- 检查上传目录状态,如有错误则显示 wp_admin_notice() 错误信息。
- 表单包含必要的安全措施,如 wp_nonce_url() 和 esc_url()。
- 相关函数包括 wp_upload_dir()、size_format() 和 submit_button()。
代码示例
function wp_import_upload_form( $action ) {
$bytes = apply_filters( 'import_upload_size_limit', wp_max_upload_size() );
$size = size_format( $bytes );
$upload_dir = wp_upload_dir();
if ( ! empty( $upload_dir['error'] ) ) :
// 显示错误信息
else :
// 输出表单 HTML
endif;
}注意事项
- 确保上传目录可写,否则函数会显示错误。
- 使用过滤器 import_upload_size_limit 可自定义最大上传大小。
- 表单 action 参数为必需,用于指定表单提交目标。
Outputs the form used by the importers to accept the data to be imported.
Parameters
$actionstringrequired-
The action attribute for the form.
Source
function wp_import_upload_form( $action ) {
/**
* Filters the maximum allowed upload size for import files.
*
* @since 2.3.0
*
* @see wp_max_upload_size()
*
* @param int $max_upload_size Allowed upload size. Default 1 MB.
*/
$bytes = apply_filters( 'import_upload_size_limit', wp_max_upload_size() );
$size = size_format( $bytes );
$upload_dir = wp_upload_dir();
if ( ! empty( $upload_dir['error'] ) ) :
$upload_directory_error = '<p>' . __( 'Before you can upload your import file, you will need to fix the following error:' ) . '</p>';
$upload_directory_error .= '<p><strong>' . $upload_dir['error'] . '</strong></p>';
wp_admin_notice(
$upload_directory_error,
array(
'additional_classes' => array( 'error' ),
'paragraph_wrap' => false,
)
);
else :
?>
<form enctype="multipart/form-data" id="import-upload-form" method="post" class="wp-upload-form" action="<?php echo esc_url( wp_nonce_url( $action, 'import-upload' ) ); ?>">
<p>
%s</label> (%s)',
__( 'Choose a file from your computer:' ),
/* translators: %s: Maximum allowed file size. */
sprintf( __( 'Maximum size: %s' ), $size )
);
?>
<input type="file" id="upload" name="import" size="25" />
<input type="hidden" name="action" value="save" />
<input type="hidden" name="max_file_size" value="<?php echo $bytes; ?>" />
</p>
</form>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/includes/template.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/template.php#L998">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/template.php#L998-L1042">View on GitHub</a></p></section>
<section class="wp-block-wporg-code-reference-hooks"><h2 id="hooks" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#hooks">Hooks</a></h2> <dl><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/import_upload_size_limit/"><span class="hook-func">apply_filters</span>( ‘import_upload_size_limit’, <nobr><span class="arg-type">int</span> <span class="arg-name">$max_upload_size</span></nobr> )</a></dt><dd><p>Filters the maximum allowed upload size for import files.</p>
</dd></dl></section>
<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/wp_admin_notice/">wp_admin_notice()</a><code>wp-includes/functions.php
Outputs an admin notice.
submit_button()wp-admin/includes/template.php
Echoes a submit button, with provided text and appropriate class(es).
wp_upload_dir()wp-includes/functions.php
Returns an array containing the current upload directory’s path and URL.
size_format()wp-includes/functions.php
Converts a number of bytes to the largest unit the bytes will fit into.
wp_max_upload_size()wp-includes/media.php
Determines the maximum upload size allowed in php.ini.
__()wp-includes/l10n.php
Retrieves the translation of $text.
esc_url()wp-includes/formatting.php
Checks and cleans a URL.
wp_nonce_url()wp-includes/functions.php
Retrieves URL with nonce added to URL query.
apply_filters()wp-includes/plugin.php
Calls the callback functions that have been added to a filter hook.
Changelog
| Version | Description |
|---|---|
| 2.0.0 | Introduced. |