File Size and Dimensions Columns in the Media Library
Adds File Size and Dimensions columns to the Media Library list view, making heavy images easy to spot.
Opens WPIDE on
Code
add_filter( 'manage_media_columns', function ( $columns ) {
$columns['wpide_filesize'] = 'File Size';
$columns['wpide_dimensions'] = 'Dimensions';
return $columns;
} );
add_action( 'manage_media_custom_column', function ( $column, $attachment_id ) {
$meta = wp_get_attachment_metadata( $attachment_id );
if ( 'wpide_filesize' === $column ) {
$bytes = ! empty( $meta['filesize'] ) ? (int) $meta['filesize'] : 0;
if ( ! $bytes ) {
$file = get_attached_file( $attachment_id );
$bytes = ( $file && file_exists( $file ) ) ? (int) filesize( $file ) : 0;
}
echo $bytes ? esc_html( size_format( $bytes, 1 ) ) : '—';
} elseif ( 'wpide_dimensions' === $column ) {
echo ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) ? esc_html( $meta['width'] . ' x ' . $meta['height'] ) : '—';
}
}, 10, 2 );
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.