WordPress Help Articles

WordPress Folders & Database Sizes

WordPress Folders & Database Sizes

How to Use

Download > Install > Activate. Click on the new admin sidebar icon labeled “WP Size”.

Example

Get the total size of your WordPress install with folder breakdowns.

Screenshot 20240501 225951

Source

This code can be pasted into the functions.php file of your child theme or, download and install the plugin for simple code activation.

function viawebs_get_database_size() {
    global $wpdb;
    $sql = "SELECT SUM(data_length + index_length) AS size FROM information_schema.tables WHERE table_schema = '" . DB_NAME . "'";
    $size = $wpdb->get_var($sql);
    return $size;
}

function viawebs_calculate_directory_sizes() {
    $document_root = $_SERVER['DOCUMENT_ROOT']; 

    function viawebs_directory_size($dir) {
        $size = 0;
        foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS)) as $file) {
            $size += $file->getSize();
        }
        return $size;
    }

    function viawebs_size_to_mb($size) {
        return number_format($size / 1024 / 1024, 2);
    }

    $total_size = 0;
    $output = '<tr><td><strong>Folder</strong></td><td><strong>Size</strong></td></tr>';

    foreach (new DirectoryIterator($document_root) as $folder) {
        if ($folder->isDot() || !$folder->isDir()) continue;
        
        $folder_path = $folder->getRealPath();
        $folder_size = viawebs_directory_size($folder_path);
        $total_size += $folder_size;
        $output .= '<tr><td>' . $folder->getFilename() . '</td><td>' . viawebs_size_to_mb($folder_size) . ' MB</td></tr>';

        if ($folder->getFilename() === 'wp-content') {
            foreach (new DirectoryIterator($folder_path) as $subfolder) {
                if ($subfolder->isDot() || !$subfolder->isDir()) continue;
                
                $subfolder_size = viawebs_directory_size($subfolder->getRealPath());
                $output .= '<tr><td>    ' . $subfolder->getFilename() . '</td><td>' . viawebs_size_to_mb($subfolder_size) . ' MB</td></tr>';
            }
        }
    }

    $db_size = viawebs_get_database_size();
    $total_size += $db_size;
    $output .= '<tr><td>Database</td><td>' . viawebs_size_to_mb($db_size) . ' MB</td></tr>';

    $total_size_gb = number_format($total_size / 1024 / 1024 / 1024, 2);

    $output .= "<tr><td><strong>Total Size of Files and Database:</strong></td><td><strong>" . $total_size_gb . " GB</strong></td></tr>";

    return $output;
}

function viawebs_add_directory_sizes_admin_page() {
    add_menu_page(
        'WordPress Folders & Database Sizes',
        'WP Size',
        'manage_options',
        'wp-size',
        'directory_sizes_page_html'
    );
}

function directory_sizes_page_html() {
    if (!current_user_can('manage_options')) {
        return;
    }

    echo '<div class="wrap">';
    echo '<h1>' . esc_html(get_admin_page_title()) . '</h1>';
    echo '<table>';
    echo viawebs_calculate_directory_sizes();
    echo '</table>';
    echo '</div>';
}
add_action('admin_menu', 'viawebs_add_directory_sizes_admin_page');

Via Webs Offers WordPress Hosting & Maintenance Services for Those That Prefer to Focus on Something Other Than the Technical Aspects

Our rollover development time lets you save unused hours for future assistance. Stay updated and optimized, with the flexibility to invest in new functionality.

Article Categories

Dive in to something specific you are wanting to learn more about.

Let's Get Started!