2

This wonderful answer to a related question; that of displaying dimensions in the media library.

display image size in media library screen

is it possible to use this technique to display the file size as a column? If so how?

2 Answers 2

8

In your theme's functions.php add the following code to get the file size for all of your items in the Media library:

add_filter( 'manage_upload_columns', 'wpse_237131_add_column_file_size' );
add_action( 'manage_media_custom_column', 'wpse_237131_column_file_size', 10, 2 );

function wpse_237131_add_column_file_size( $columns ) { // Create the column
    $columns['filesize'] = 'File Size';
    return $columns;
}
function wpse_237131_column_file_size( $column_name, $media_item ) { // Display the file size
    if ( 'filesize' != $column_name || !wp_attachment_is_image( $media_item ) ) {
      return;
    }
    $filesize = filesize( get_attached_file( $media_item ) );
    $filesize = size_format($filesize, 2);
    echo $filesize;
}
6
  • Unfortunately this doesn't return the file size. It returns a number for instance "809.00 B" and each subsequent entry has a decremented number.
    – Chris Pink
    Aug 28, 2016 at 10:52
  • 809 is the attachment ID
    – Chris Pink
    Aug 28, 2016 at 10:58
  • @ChrisPink Updated my code. May you select my one as the accepted answer instead since I show the complete code instead of yours? Aug 28, 2016 at 12:12
  • Of course, how about upvoting my answer too? ;-)
    – Chris Pink
    Aug 29, 2016 at 13:31
  • Not working on WP 4.8 :(
    – juanchod
    Nov 28, 2017 at 12:45
2

Just a little bit of extra work, change the function above to this;

function wpse_237131_column_file_size( $column_name, $media_item ) { // Display the file size
if ( 'filesize' != $column_name || !wp_attachment_is_image( $media_item ) ) {
  return;
}
$filesize = filesize( get_attached_file( $media_item ) );
$filesize = size_format($filesize, 2);
echo $filesize;
}
1
  • Thanks for catching that, updated my answer with the full code. Aug 28, 2016 at 12:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.