How to Add a File Size Column in the WordPress Media Library Admin Screen
|

How to Add a File Size Column in the WordPress Media Library Admin Screen

Do you ever find yourself looking for the file size of your media files in the WordPress Media Library? By default, WordPress does not display the file size of your media files in the Media Library table. However, you can easily add a file size column to the Media Library table to make this information visible. In this post, we’ll show you how to add a file size column to the WordPress Media Library.

How to Add a File Size Column to the WordPress Media Library

To add a file size column to the WordPress Media Library, you will need to add a few lines of code to your WordPress theme’s functions.php file. Before you make any changes to your functions.php file, it’s always a good idea to make a backup copy of the file.

To get started, open your functions.php file and add the following code:

// Add a column for file size in the Media Library table
function custom_media_column_file_size( $columns ) {
    $columns['file_size'] = __( 'File Size', 'custom-media-columns' );
    return $columns;
}
add_filter( 'manage_media_columns', 'custom_media_column_file_size' );
// Display the file size in the Media Library table
function custom_media_column_file_size_data( $column_name, $attachment_id ) {
    if ( 'file_size' == $column_name ) {
        $bytes = filesize( get_attached_file( $attachment_id ) );
        echo size_format( $bytes, 2 );
    }
}
add_action( 'manage_media_custom_column', 'custom_media_column_file_size_data', 10, 2 );

Once you have added this code, save the file and refresh the Media Library page in your WordPress dashboard. You should now see a new column labeled “File Size” in the Media Library table, which displays the file size of each media file in a human-readable format.

How This Code Works

Let’s break down what the code above is doing.

First, we use the manage_media_columns filter to add a new column to the Media Library table. The custom_media_column_file_size function adds a new column called “File Size” to the $columns array.

Next, we use the manage_media_custom_column action to display the file size in the Media Library table. The custom_media_column_file_size_data function checks if the current column is “File Size”, gets the file size of the media file using the filesize() function, and then displays the file size in a human-readable format using the size_format() function.

Conclusion

Adding a file size column to the WordPress Media Library can be a useful way to make important information more visible and accessible. By adding just a few lines of code to your functions.php file, you can easily display the file size of each media file in the Media Library table. We hope this tutorial has been helpful for you, and if you have any questions or feedback, feel free to leave a comment below!

Similar Posts

Leave a Reply

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