Ever wondered how large your uploaded files are in WordPress? When you’re managing limited server storage or trying to improve website performance, seeing file sizes directly inside the Media Library can save time.
In this tutorial, you’ll learn how to:
- Add a “File Size” column to your WordPress Media Library.
- Display the actual size of each media file.
- (Bonus) Make that column sortable, so you can easily find the largest or smallest files.
Skill Level: Beginner to Intermediate
🧰 What You’ll Need
- Access to your WordPress Admin Dashboard
- Basic knowledge of how to edit the functions.php file (
Appearance → Theme File Editor) - (Optional but recommended) Use a child theme or a code snippets plugin (like Code Snippets) to keep your customizations safe from theme updates.
⚠️ Before You Begin: Always Back Up
Before editing functions.php, create a full backup of your website. Even a small syntax error can cause your site to break, so it’s best to play it safe.
Step 1: Add the “File Size” Column
Add the following code to your functions.php file or use your code snippets plugin:
// 1. Add a File Size column to the Media Library
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' );
🔍 What this does:
- Adds a new column labeled “File Size” in the Media Library list view.
- The
__()function ensures the label can be translated into other languages.
(Note: This only creates the column. It won’t display any file size data yet — we’ll handle that next.)
Step 2: Display Each File’s Size
Now, let’s fill that new column with the actual file sizes:
// 2. Display file size for each media item
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 );
🧠 How it works:
get_attached_file()retrieves the file path.filesize()returns the size in bytes.size_format()converts bytes into a human-readable format (KB, MB, etc.).
At this point, your Media Library will display a “File Size” column showing how large each file is.
💪 Bonus: Make the File Size Column Sortable
If you want to sort files from smallest to largest (or vice versa), extend your code with this snippet:
// 3. Register the File Size column as sortable
function custom_media_column_sortable( $columns ) {
$columns['file_size'] = 'file_size';
return $columns;
}
add_filter( 'manage_upload_sortable_columns', 'custom_media_column_sortable' );
// 4. Adjust the query for sorting
function custom_media_column_orderby( $query ) {
if ( ! is_admin() || ! $query->is_main_query() ) return;
if ( 'file_size' === $query->get( 'orderby' ) ) {
$query->set( 'meta_key', '_custom_file_size' );
$query->set( 'orderby', 'meta_value_num' );
}
}
add_action( 'pre_get_posts', 'custom_media_column_orderby' );
// 5. Store file size as post meta on upload/edit
function save_custom_file_size_meta( $post_ID ) {
$bytes = filesize( get_attached_file( $post_ID ) );
update_post_meta( $post_ID, '_custom_file_size', $bytes );
}
add_action( 'add_attachment', 'save_custom_file_size_meta' );
add_action( 'edit_attachment', 'save_custom_file_size_meta' );
🧩 What this extended code does:
- Makes the File Size column sortable in the Media Library.
- Tells WordPress to use a custom meta key (
_custom_file_size) for sorting. - Saves file size information whenever new files are uploaded or edited.
📦 Why use post meta?
Sorting requires the file size to be stored in the database. Saving it as meta data allows WordPress to sort numerically using meta_value_num.
✅ Final Result
After adding all the snippets:
- You’ll see a File Size column in your Media Library’s List View.
- Each file shows its size (e.g., 256 KB or 2.3 MB).
- Clicking the column header will sort files by size — ascending or descending.
🧰 Troubleshooting Tips
- If the column doesn’t appear, switch to List View (not Grid View).
- If sorting doesn’t work, ensure file sizes were saved as post meta. You might need to re-save or re-upload old media.
- For older uploads, use tools like Regenerate Thumbnails or a custom batch script to refresh metadata.
⚙️ Alternative Plugin Option
If you’d prefer a no-code method, plugins such as Media Library Assistant can handle advanced sorting, filtering, and searching.
However, for lightweight customization, adding your own code is faster and keeps your site lean.
🎯 Conclusion
Adding a File Size column to your WordPress Media Library gives you more control over your media assets. It helps you:
- Stay within hosting storage limits.
- Identify large files that may slow down your site.
- Keep your media library clean and optimized.
With just a few simple snippets, you can make file management in WordPress smarter, cleaner, and more efficient.
📌 Post Tags:
#WordPressTips #MediaLibrary #FileManagement #SortableColumns #CustomAdminColumns #WordPressOptimization