Home / WORDPRESS / How to Insert a File Size Column in the WordPress Media Library

How to Insert a File Size Column in the WordPress Media Library

The WordPress Media Library serves as a tool for organizing images, videos, and various files, yet lacks the feature to exhibit file sizes by default. Recognizing the file size of media files proves beneficial for enhancing website performance and managing storage capacity. This tutorial will demonstrate the process of adding a file size column in the WordPress Media Library through a straightforward code snippet.

Significance of Inserting a File Size Column

Incorporating a file size column can

  • Enable monitoring of large media files that might impede website speed.

  • Facilitate management of storage constraints on your hosting platform.

  • Enhance overall site performance by optimizing images and media files.

Incorporating a File Size Column through Coding

To introduce a file size column, implement a few lines of code into your theme’s functions.php file or utilize a code manager plugin like WP Code. Adhere to these steps meticulously:

Step 1: Access the Theme Functions File

  1. Log into your WordPress admin dashboard.

  2. Proceed to Appearance > Theme Editor.

  3. Locate and open the functions.php file from your active theme.

Step 2: Integrate the Following Code

Append the provided code snippet at the conclusion of the functions.php file:

add_filter( 'manage_upload_columns', function ( $columns ) {
	$columns ['file_size'] = esc_html__( 'File size' );

	return $columns;
} );

add_action( 'manage_media_custom_column', function ( $column_name, $media_item ) {
	if ( 'file_size' !== $column_name || ! wp_attachment_is_image( $media_item ) ) {
		return;
	}
	$filesize = size_format( filesize( get_attached_file( $media_item ) ), 2 );
	echo esc_html( $filesize );

}, 10, 2 );

Step 3: Save the File

Upon inserting the code, select the Update File option to preserve your modifications.

Note: When utilizing a code manager plugin, ensure to apply this code snippet via the PHP option.

Alternative Approach: Utilize a Plugin

If you prefer to refrain from code editing, consider employing a plugin such as Media Library File Size for displaying file sizes without altering theme files.

Conclusion

Incorporating a file size column in the WordPress Media Library streamlines the management of media files. Whether opting for a custom code snippet or a plugin, this uncomplicated integration enhances workflow efficiency and promotes the upkeep of a swift and optimized website.

Leave a Reply

Your email address will not be published. Required fields are marked *