upload_space_setting()
概述
upload_space_setting() 函数用于在编辑站点设置屏幕中显示上传空间配额设置表单。它通过切换博客上下文获取配额选项,并输出相应的 HTML 表单元素。
关键要点
- 函数接受一个必需的整数参数 $id,指定要显示设置的站点 ID。
- 使用 switch_to_blog() 和 restore_current_blog() 切换博客上下文以获取 blog_upload_space 选项值。
- 输出包含配额输入框和标签的 HTML 表单,使用 esc_attr() 进行属性转义,_e() 进行文本翻译。
- 自 WordPress 3.0.0 版本引入,属于多站点功能的一部分。
代码示例
function upload_space_setting( $id ) {
switch_to_blog( $id );
$quota = get_option( 'blog_upload_space' );
restore_current_blog();
if ( ! $quota ) {
$quota = '';
}
?>
<label for="blog-upload-space-number"><?php _e( 'Site Upload Space Quota' ); ?></label>
<input name="option[blog_upload_space]" type="number" step="1" min="0" id="blog-upload-space-number" aria-describedby="blog-upload-space-desc" value="<?php echo esc_attr( $quota ); ?>" />
<?php
}注意事项
- 此函数主要用于多站点环境,依赖于 switch_to_blog() 和 restore_current_blog() 来正确访问指定站点的选项。
- 配额值通过 get_option() 获取,如果未设置则默认为空字符串,确保表单输入框显示正确。
- 输出时使用 esc_attr() 防止 XSS 攻击,_e() 确保国际化支持,开发者应遵循这些安全最佳实践。
Displays the site upload space quota setting form on the Edit Site Settings screen.
Parameters
$idintrequired-
The ID of the site to display the setting for.
Source
function upload_space_setting( $id ) {
switch_to_blog( $id );
$quota = get_option( 'blog_upload_space' );
restore_current_blog();
if ( ! $quota ) {
$quota = '';
}
?>
<tr>
<th><label for="blog-upload-space-number"></label></th>
<td>
<input type="number" step="1" min="0" style="width: 100px"
name="option[blog_upload_space]" id="blog-upload-space-number"
aria-describedby="blog-upload-space-desc" value="<?php echo esc_attr( $quota ); ?>" />
<span id="blog-upload-space-desc"><span class="screen-reader-text">
</span> </span>
</td>
</tr>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/includes/ms.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/ms.php#L295">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/ms.php#L295-L320">View on GitHub</a></p></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/switch_to_blog/">switch_to_blog()</a><code>wp-includes/ms-blogs.php
Switches the current blog.
restore_current_blog()wp-includes/ms-blogs.php
Restores the current blog, after calling switch_to_blog() .
_e()wp-includes/l10n.php
Displays translated text.
esc_attr()wp-includes/formatting.php
Escaping for HTML attributes.
get_option()wp-includes/option.php
Retrieves an option value based on an option name.
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |