The WPFolks app is out on iPhone and Android. Free, no ads. Download on the App Store Get it on Google Play
Skip to main content
Login Join
Snippet · PHP

Display Image Dimensions and File Size in the WordPress Media Library

Shared by Alkesh Miyani · November 25, 2026 · @manage_media_columns, manage_media_custom_column

14 views
1 upvote
Back to Snippets

Managing images and media files becomes easier when you can see their dimensions and file sizes directly in the WordPress Media Library.

This snippet adds two columns to the Media Library’s list view:

This helps developers and content managers identify large images and review media specifications without opening individual attachment details.

How to use

  1. Add the code to your child theme’s functions.php file or a PHP snippets plugin.
  2. Navigate to WordPress Admin → Media → Library.
  3. Switch to List View if you’re currently using Grid View.
  4. Look for the new Image Dimensions and File Size columns.

Example output

Media file Image Dimensions File Size
homepage-banner.jpg 1920 × 1080 px 245 KB
company-logo.png 500 × 500 px 85 KB
product-photo.webp 1200 × 1200 px 156 KB
brochure.pdf — 2.45 MB

Example data only.

Notes

/**
 * Add image dimensions and file size columns to Media Library.
 */
add_filter(
	'manage_media_columns',
	'alkesh_add_media_details_columns'
);

function alkesh_add_media_details_columns( $columns ) {
	$columns['alkesh_dimensions'] = __( 'Image Dimensions', 'alkesh' );
	$columns['alkesh_file_size']  = __( 'File Size', 'alkesh' );

	return $columns;
}

/**
 * Display image dimensions and file size.
 */
add_action(
	'manage_media_custom_column',
	'alkesh_display_media_details_columns',
	10,
	2
);

function alkesh_display_media_details_columns( $column_name, $attachment_id ) {
	if ( ! in_array(
		$column_name,
		array( 'alkesh_dimensions', 'alkesh_file_size' ),
		true
	) ) {
		return;
	}

	$metadata = wp_get_attachment_metadata( $attachment_id );

	// Display image dimensions.
	if ( 'alkesh_dimensions' === $column_name ) {
		if (
			wp_attachment_is_image( $attachment_id ) &&
			! empty( $metadata['width'] ) &&
			! empty( $metadata['height'] )
		) {
			printf(
				'%1$s × %2$s px',
				esc_html( number_format_i18n( $metadata['width'] ) ),
				esc_html( number_format_i18n( $metadata['height'] ) )
			);
		} else {
			echo '—';
		}

		return;
	}

	// Retrieve file size from attachment metadata.
	$file_size = isset( $metadata['filesize'] )
		? absint( $metadata['filesize'] )
		: 0;

	// Fall back to the actual file size when metadata is unavailable.
	if ( ! $file_size ) {
		$file_path = get_attached_file( $attachment_id );

		if ( $file_path && is_file( $file_path ) ) {
			$file_size = filesize( $file_path );
		}
	}

	if ( false === $file_size || ! $file_size ) {
		echo '—';
		return;
	}

	echo esc_html( size_format( $file_size, 2 ) );
}

Tested on

WordPress 7.1.5 WordPress Admin

Reported by the author on November 25, 2026. WPFolks has not verified this.

Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments

Proudly supported by