函数文档

wp_dashboard_quota()

💡 云策文档标注

概述

wp_dashboard_quota() 函数用于在 WordPress 仪表盘中显示文件上传配额信息。它通过 'activity_box_end' Hook 在 wp_dashboard_right_now() 中运行,主要处理多站点环境下的存储空间检查。

关键要点

  • 函数在非多站点、用户无 upload_files 权限或 upload_space_check_disabled 选项启用时返回 true。
  • 使用 get_space_allowed() 和 get_space_used() 获取配额和已用空间,计算使用百分比。
  • 当使用率超过 70% 时添加 'warning' CSS 类以高亮显示。
  • 输出包含配额详情和管理上传链接的 HTML 内容。

代码示例

function wp_dashboard_quota() {
    if ( ! is_multisite() || ! current_user_can( 'upload_files' )
        || get_site_option( 'upload_space_check_disabled' )
    ) {
        return true;
    }

    $quota = get_space_allowed();
    $used  = get_space_used();

    if ( $used > $quota ) {
        $percentused = '100';
    } else {
        $percentused = ( $used / $quota ) * 100;
    }

    $used_class  = ( $percentused >= 70 ) ? ' warning' : '';
    $used        = round( $used, 2 );
    $percentused = number_format( $percentused );

    ?>
    <h3 class="mu-storage"><?php _e( 'Storage Space' ); ?></h3>
    <div class="mu-storage">
    <ul>
        <li class="storage-count<?php echo $used_class; ?>">
            <?php
            $text = sprintf(
                /* translators: 1: Number of megabytes, 2: Percentage. */
                __( '%1$s MB of %2$s MB (%3$s%%) used' ),
                number_format_i18n( $used ),
                number_format_i18n( $quota ),
                $percentused
            );
            printf(
                '<a href="%1$s">%2$s <span class="screen-reader-text">(%3$s)</span></a>',
                esc_url( admin_url( 'upload.php' ) ),
                $text,
                /* translators: Hidden accessibility text. */
                __( 'Manage Uploads' )
            );
            ?>
        </li>
    </ul>
    </div>
    <?php
}

注意事项

  • 此函数仅适用于 WordPress 多站点环境,单站点或无权限用户不会显示配额信息。
  • 使用前需确保相关 Hook 和函数如 get_space_allowed() 已正确配置。

📄 原文内容

Displays file upload quota on dashboard.

Description

Runs on the ‘activity_box_end’ hook in wp_dashboard_right_now() .

Return

true|void True if not multisite, user can’t upload files, or the space check option is disabled.

Source

function wp_dashboard_quota() {
if ( ! is_multisite() || ! current_user_can( 'upload_files' )
|| get_site_option( 'upload_space_check_disabled' )
) {
return true;
}

$quota = get_space_allowed();
$used = get_space_used();

if ( $used > $quota ) {
$percentused = '100';
} else {
$percentused = ( $used / $quota ) * 100;
}

$used_class = ( $percentused >= 70 ) ? ' warning' : '';
$used = round( $used, 2 );
$percentused = number_format( $percentused );

?>
<h3 class="mu-storage"></h3>
<div class="mu-storage">
<ul>
<li class="storage-count">
%2$s<span class="screen-reader-text"> (%3$s)</span></a>',
esc_url( admin_url( 'upload.php' ) ),
$text,
/* translators: Hidden accessibility text. */
__( 'Manage Uploads' )
);
?>
</li><li class="storage-count <?php echo $used_class; ?>">
%2$s<span class="screen-reader-text"> (%3$s)</span></a>',
esc_url( admin_url( 'upload.php' ) ),
$text,
/* translators: Hidden accessibility text. */
__( 'Manage Uploads' )
);
?>
</li>
</ul>
</div>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/includes/dashboard.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/dashboard.php#L1643">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/dashboard.php#L1643-L1702">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/get_space_allowed/">get_space_allowed()</a><code>wp-includes/ms-functions.php

Returns the upload quota for the current blog.

get_space_used()wp-includes/ms-functions.php

Returns the space used by the current site.

current_user_can()wp-includes/capabilities.php

Returns whether the current user has the specified capability.

_e()wp-includes/l10n.php

Displays translated text.

__()wp-includes/l10n.php

Retrieves the translation of $text.

esc_url()wp-includes/formatting.php

Checks and cleans a URL.

is_multisite()wp-includes/load.php

Determines whether Multisite is enabled.

number_format_i18n()wp-includes/functions.php

Converts float number to format based on the locale.

admin_url()wp-includes/link-template.php

Retrieves the URL to the admin area for the current site.

get_site_option()wp-includes/option.php

Retrieve an option value for the current network based on name of option.

Show 8 moreShow less

Changelog

Version Description
3.0.0 Introduced.