Using WordPress ‘display_space_usage()’ PHP function

The display_space_usage() WordPress PHP function displays the amount of disk space used by the current WordPress site. It’s important to note that this function isn’t used in the core WordPress software.

Usage

To display the disk space used by your WordPress site, you simply call the function like this:

display_space_usage();

This will print the disk space used by your site in bytes.

Parameters

  • The display_space_usage() function does not take any parameters.

More information

See WordPress Developer Resources: display_space_usage()

Examples

Display Space Usage in a Dashboard Widget

If you want to keep track of your disk usage, you can create a simple dashboard widget that uses display_space_usage().

function add_disk_usage_widget() {
    wp_add_dashboard_widget('disk_usage', 'Disk Usage', 'display_space_usage');
}
add_action('wp_dashboard_setup', 'add_disk_usage_widget');

In this code, we’re using the wp_add_dashboard_widget() function to create a new widget in the WordPress admin dashboard that displays the disk usage of the site.

You may want to display your site’s disk usage in the footer of your site. Here’s how you might do that with display_space_usage():

function display_footer_space_usage() {
    echo 'Disk usage: ';
    display_space_usage();
}
add_action('wp_footer', 'display_footer_space_usage');

This code hooks into the wp_footer action, which runs just before the closing </body> tag in your WordPress theme.

Display Space Usage in Bytes, Kilobytes, and Megabytes

While display_space_usage() returns the disk usage in bytes, you may prefer to see this information in kilobytes or megabytes. Here’s how you can do that:

function display_space_usage_kb_mb() {
    $bytes = get_space_used();
    echo 'Disk usage: ' . $bytes . ' bytes, ' . ($bytes / 1024) . ' KB, ' . ($bytes / 1048576) . ' MB';
}
display_space_usage_kb_mb();

In this example, we’re using the get_space_used() function, which returns the same information as display_space_usage(), but without printing it. We then convert this value to kilobytes and megabytes by dividing by 1024 and 1048576, respectively.

Display Space Usage in a Post

You can also display your site’s disk usage in a post using a shortcode. Here’s how you can create a [disk_usage] shortcode that does this:

function disk_usage_shortcode() {
    ob_start();
    display_space_usage();
    return ob_get_clean();
}
add_shortcode('disk_usage', 'disk_usage_shortcode');

To use the shortcode, simply insert [disk_usage] into any post or page. The shortcode will be replaced with the site’s disk usage when the post or page is viewed.

Display Space Usage in the Admin Bar

Lastly, you might want to display your site’s disk usage in the WordPress admin bar. Here’s how you can do that:

function add_admin_bar_space_usage($wp_admin_bar) {
    ob_start();
    display_space_usage();
    $usage = ob_get_clean();

    $args = array(
        'id'    => 'disk_usage',
        'title' => 'Disk usage: ' . $usage,
    );
    $wp_admin_bar->add_node($args);
}
add_action('admin_bar_menu', 'add_admin_bar_space_usage', 999);

In this code, we’re using the admin_bar_menu action to add a new item to the admin bar. The add_node() method is used to add the new item, which displays the disk usage of the site. The ‘999’ at the end of the add_action() function call is the priority, ensuring our function runs late, after other items have been added to the admin bar.