Last active
March 16, 2021 13:00
-
-
Save IlanVivanco/92ee4dfdb341435abb871820e0e60242 to your computer and use it in GitHub Desktop.
Media library size Dashboard Widget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'wp_dashboard_setup', 'iv_wp_dashboard_setup' ); | |
function iv_wp_dashboard_setup() { | |
if( current_user_can( 'install_plugins' ) ) | |
wp_add_dashboard_widget( 'iv_folder_sizes', __( 'Folder Sizes' ), 'iv_wp_add_dashboard_widget' ); | |
} | |
function iv_wp_add_dashboard_widget() { | |
$upload_dir = wp_upload_dir(); | |
$upload_space = iv_foldersize( $upload_dir['basedir'] ); | |
$content_space = iv_foldersize( WP_CONTENT_DIR ); | |
$wp_space = iv_foldersize( ABSPATH ); | |
/* ABSOLUTE paths not being shown in Widget */ | |
echo '<i>Uploads</i>: ' . iv_format_size( $upload_space ) . '<br>'; | |
echo '<i>wp-content</i>: ' . iv_format_size( $content_space ) . '<br>'; | |
if( is_multisite() ) { | |
echo '<i>wp-content/blogs.dir</i>: ' . iv_format_size( iv_foldersize( WP_CONTENT_DIR . '/blogs.dir' ) ) . '<br>'; | |
} | |
echo '<i>WordPress</i>: ' . iv_format_size( $wp_space ); | |
} | |
function iv_foldersize( $path ) { | |
$total_size = 0; | |
$files = scandir( $path ); | |
$clean_path = rtrim( $path, '/' ) . '/'; | |
foreach( $files as $t ) { | |
if ( '.' != $t && '..' != $t ) { | |
$current_file = $clean_path . $t; | |
if ( is_dir( $current_file ) ) { | |
$size = iv_foldersize( $current_file ); | |
$total_size += $size; | |
} else { | |
$size = filesize( $current_file ); | |
$total_size += $size; | |
} | |
} | |
} | |
return $total_size; | |
} | |
function iv_format_size($size) { | |
$units = explode( ' ', 'B KB MB GB TB PB' ); | |
$mod = 1024; | |
for ( $i = 0; $size > $mod; $i++ ) | |
$size /= $mod; | |
$end_index = strpos( $size, "." ) + 3; | |
return substr( $size, 0, $end_index ) . ' ' . $units[$i]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment