Opened 3 years ago
Last modified 22 months ago
#55261 new feature request
Add a "file size" column to the media library menu
Reported by: | siyavashebr | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | |
Component: | Media | Keywords: | has-screenshots needs-docs has-patch |
Focuses: | administration | Cc: |
Description
One of the things I think should be placed in the core of WordPress is to display the size of various photos and files that are uploaded to the host through WordPress in the media library menu (as a column in the table to make comparison easier). After a while and by adding different files, we do not know which file gets the most size from the host and in general it is very good that the webmaster can easily view the size of different files to make site management easier because according to the experience, This Menu is one of the most sensitive WordPress menus and must be carefully monitored. You can add this feature using the code below ;
<?php class MediaLibraryFeatures { private static $instance = null; /** * Get single instance of this class - Singleton * * @since 1.0 */ public static function getInstance() { if ( ! self::$instance ) { self::$instance = new self(); } return self::$instance; } /** * MediaLibrary constructor. */ private function __construct() { $this->initHooks(); } /** * Initialize Hooks. * * @author Siavash Ebrahimi * @since 1.0 * @return void */ public function initHooks() { // Register size column for media library table add_filter( 'manage_media_columns', [ $this, 'registerSizeColumn' ], 15 ); add_action( 'manage_media_custom_column', [ $this, 'populateCustomColumns' ], 15, 2 ); add_action( 'added_post_meta', [ $this, 'addFileSizeMetadataToImages' ], 15, 4 ); add_filter( 'manage_upload_sortable_columns', [ $this, 'registerColumnSortableFileSize' ], 15 ); add_action( 'pre_get_posts', [ $this, 'sortableFileSizeSortingLogic' ], 15 ); } /** * Column sorting logic (query modification) * * @param $query */ public function sortableFileSizeSortingLogic( $query ) { global $pagenow; if ( is_admin() && 'upload.php' == $pagenow && $query->is_main_query() && ! empty( $_REQUEST['orderby'] ) && 'filesize' == $_REQUEST['orderby'] ) { $query->set( 'order', 'ASC' ); $query->set( 'orderby', 'meta_value_num' ); $query->set( 'meta_key', 'filesize' ); if ( 'desc' == strtolower($_REQUEST['order']) ) { $query->set( 'order', 'DSC' ); } } } /** * Make column sortable * * @param $columns * * @return mixed */ public function registerColumnSortableFileSize( $columns ) { $columns['filesize'] = 'filesize'; return $columns; } /** * Ensure file size meta gets added to new uploads * * @param $meta_id * @param $post_id * @param $meta_key * @param $meta_value */ public function addFileSizeMetadataToImages( $meta_id, $post_id, $meta_key, $meta_value ) { if ( '_wp_attachment_metadata' == $meta_key ) { $file = get_attached_file( $post_id ); update_post_meta( $post_id, 'filesize', filesize( $file ) ); } } /** * Populate media custom columns * * @return string * @since 1.0 * @access public */ public function populateCustomColumns( $column_name, $post_id ) { if ( 'filesize' == $column_name ) { if ( ! get_post_meta( $post_id, 'filesize', true ) ) { $file = get_attached_file( $post_id ); $file_size = filesize( $file ); update_post_meta( $post_id, 'filesize', $file_size ); } else { $file_size = get_post_meta( $post_id, 'filesize', true ); } echo size_format( $file_size, 2 ); } } /** * Register size column * * @param array $cols * * @return mixed * @since 1.0 */ public function registerSizeColumn( $cols ) { $cols['filesize'] = __( 'File Size' ); return $cols; } }
Then initialize the class :
<?php MediaLibraryFeatures::getInstance();
Attachments (1)
Change History (7)
This ticket was mentioned in PR #2362 on WordPress/wordpress-develop by siaeb.
3 years ago
#1
- Keywords has-patch added; needs-patch removed
#3
follow-up:
↓ 4
@
3 years ago
It looks like the PR has quite a few unintended changes in 192 files, could you make sure it's based on current trunk and not on the 5.9 branch?
#4
in reply to:
↑ 3
@
3 years ago
Replying to SergeyBiryukov: Yes it is
It looks like the PR has quite a few unintended changes in 192 files, could you make sure it's based on current trunk and not on the 5.9 branch?
#5
@
3 years ago
It is worth noting that in 6.0, file size will be stored in as part of post meta. There is also a new helper function called, wp_filesize
. See #49412 for more information.
#6
@
22 months ago
In WP 6.1.1 the Media Library in list view has a "Size" (=file size) column which can be toggled via "Screen Options".
So I think this ticket can be closed. The feature is in core (whether from this contribution or another I dunno).
Glad that this is now available! Thanks to everyone involved.
One of the things I think should be placed in the core of WordPress is to display the size of various photos and files that are uploaded to the host through WordPress in the media library menu (as a column in the table to make comparison easier). After a while and by adding different files, we do not know which file gets the most size from the host and in general it is very good that the webmaster can easily view the size of different files to make site management easier because according to the experience, This Menu is one of the most sensitive WordPress menus and must be carefully monitored. You can add this feature using the code below ;
Trac ticket: [](https://core.trac.wordpress.org/ticket/55261)