Using WordPress ‘get_space_used()’ PHP function

The get_space_used() WordPress PHP function returns the space used by the current site in megabytes.

Usage

$used_space = get_space_used();
echo "The current site is using " . $used_space . " MB.";

Parameters

  • None

More information

See WordPress Developer Resources: get_space_used()

Examples

Display Space Used in the Admin Dashboard

Display the space used by the current site in the admin dashboard.

function display_space_used() {
  $used_space = get_space_used();
  echo '<div class="notice notice-info">';
  echo '<p>Current site is using ' . $used_space . ' MB of space.</p>';
  echo '</div>';
}
add_action('admin_notices', 'display_space_used');

Show the space used by the current site in the footer.

function add_space_used_to_footer() {
  $used_space = get_space_used();
  echo '<p>Space Used: ' . $used_space . ' MB</p>';
}
add_action('wp_footer', 'add_space_used_to_footer');

Send Space Used Notification via Email

Send an email notification if the space used is greater than a specified limit.

function notify_space_used() {
  $used_space = get_space_used();
  $limit = 500; // Set the limit in MB

  if ($used_space > $limit) {
    $to = '[email protected]';
    $subject = 'Space Used Alert';
    $message = 'The current site is using ' . $used_space . ' MB. Limit: ' . $limit . ' MB.';
    wp_mail($to, $subject, $message);
  }
}
add_action('init', 'notify_space_used');

Create a Shortcode to Display Space Used

Create a shortcode [space_used] to display the space used by the current site.

function space_used_shortcode() {
  $used_space = get_space_used();
  return 'Space Used: ' . $used_space . ' MB';
}
add_shortcode('space_used', 'space_used_shortcode');

Display Space Used with a Progress Bar

Show the space used by the current site with a progress bar.

function display_space_used_progress_bar() {
  $used_space = get_space_used();
  $max_space = 1000; // Set the maximum allowed space in MB
  $percentage = ($used_space / $max_space) * 100;

  echo '<div style="background-color: #eee; width: 100%; height: 30px; position: relative;">';
  echo '<div style="background-color: #0073aa; width: ' . $percentage . '%; height: 30px;"></div>';
  echo '<div style="position: absolute; top: 0; left: 0; right: 0; bottom: 0; text-align: center; font-weight: bold;">' . $used_space . ' MB / ' . $max_space . ' MB</div>';
  echo '</div>';
}
add_action('wp_footer', 'display_space_used_progress_bar');