1

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?

3

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;
}
  • 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 '16 at 10:52
  • 809 is the attachment ID – Chris Pink Aug 28 '16 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? – Ethan Jinks O'Sullivan Aug 28 '16 at 12:12
  • Of course, how about upvoting my answer too? ;-) – Chris Pink Aug 29 '16 at 13:31
  • Not working on WP 4.8 :( – juanchod Nov 28 '17 at 12:45
1

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;
}
  • Thanks for catching that, updated my answer with the full code. – Ethan Jinks O'Sullivan Aug 28 '16 at 12:33

protected by Community Dec 21 '18 at 1:28

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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