Media PHP Beginner

Add File Size and Dimensions Columns to the Media Library

Show each file's size and image dimensions in the Media Library list view, so you can spot oversized uploads at a glance.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 23 lines
add_filter( 'manage_media_columns', function ( array $columns ): array {
	$columns['nahnu_filesize']   = 'File size';
	$columns['nahnu_dimensions'] = 'Dimensions';
	return $columns;
} );

add_action( 'manage_media_custom_column', function ( string $column, int $post_id ): void {
	$meta = wp_get_attachment_metadata( $post_id );

	if ( 'nahnu_filesize' === $column ) {
		$bytes = $meta['filesize'] ?? 0;
		if ( ! $bytes ) {
			$file  = get_attached_file( $post_id );
			$bytes = $file && file_exists( $file ) ? filesize( $file ) : 0;
		}
		$size = $bytes ? size_format( $bytes, 1 ) : '—';
		echo $bytes > MB_IN_BYTES ? '<strong style="color:#b32d2e">' . esc_html( $size ) . '</strong>' : esc_html( $size );
	}

	if ( 'nahnu_dimensions' === $column ) {
		echo isset( $meta['width'], $meta['height'] ) ? esc_html( $meta['width'] . ' × ' . $meta['height'] ) : '—';
	}
}, 10, 2 );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

The Media Library doesn’t show file sizes in its list, so a 12 MB photo looks the same as a 60 KB one. This snippet adds two columns and highlights anything over 1 MB.

How it works

manage_media_columns adds the columns and manage_media_custom_column fills them for each file. Since WordPress 6.0 the file size is stored in the attachment metadata. For older uploads the snippet reads it from the file on disk.

Image width and height also come from the metadata. Files over 1 MB are shown in bold red.

Things to watch

  • The columns only appear in the list view. Switch with the list icon at the top of Media → Library.
  • Hide either column under Screen Options.

How to test it

Go to Media → Library and switch to list view. You should see File size and Dimensions columns.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only