Looking to add file size column in the WordPress media library?
Add the following in child theme’s functions.php:
This file contains 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
add_filter( 'manage_media_columns', 'sk_media_columns_filesize' ); | |
/** | |
* Filter the Media list table columns to add a File Size column. | |
* | |
* @param array $posts_columns Existing array of columns displayed in the Media list table. | |
* @return array Amended array of columns to be displayed in the Media list table. | |
*/ | |
function sk_media_columns_filesize( $posts_columns ) { | |
$posts_columns['filesize'] = __( 'File Size', 'my-theme-text-domain' ); | |
return $posts_columns; | |
} | |
add_action( 'manage_media_custom_column', 'sk_media_custom_column_filesize', 10, 2 ); | |
/** | |
* Display File Size custom column in the Media list table. | |
* | |
* @param string $column_name Name of the custom column. | |
* @param int $post_id Current Attachment ID. | |
*/ | |
function sk_media_custom_column_filesize( $column_name, $post_id ) { | |
if ( 'filesize' !== $column_name ) { | |
return; | |
} | |
$bytes = filesize( get_attached_file( $post_id ) ); | |
echo size_format( $bytes, 2 ); | |
} | |
add_action( 'admin_print_styles-upload.php', 'sk_filesize_column_filesize' ); | |
/** | |
* Adjust File Size column on Media Library page in WP admin | |
*/ | |
function sk_filesize_column_filesize() { | |
echo | |
'<style> | |
.fixed .column-filesize { | |
width: 10%; | |
} | |
</style>'; | |
} |
If you would like to use a plugin for the same, here are a few options:
References:
Thanks! Great snippet and works as it should. Better than a plugin. 🙂
One question: Could you please change the code, so i can sort the images by size, too?
And in addition to my previous comment. Could you please add an additional, sortable column that displays the image dimensions? 🙂
That would be very handy.
Hi,
Any update on this please?
I am looking for the same thing as Bob: the ability to make that image file size sortable.
Thanks
You might want to use Admin Columns Pro.
https://www.admincolumns.com/tips-tricks-easily-filter-big-media-files/
Thank you.
Works exactly as stated. Excellent work. Many thanks.
This did not work for me. I added a function.php to my child theme then added this code, but it didn’t change my media page layout
Works perfect, just what I was looking for. Thanks 🙂
I’m a newbie blogger, thanks for this great share, it worked exactly as shared.
didnt actually work for me though
This is working great.
Thank you 🙂