Thêm cột kích thước file trong admin media nhằm quản lý dung lượng file up lên wordpress, tránh trường hợp người dùng không biết upload các file có dung lượng lớn không cần thiết lên website.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
add_filter( 'manage_media_columns', 'codfe_media_columns_filesize' ); /** * Filter the Media list table columns to add a File Size column. * * @param array $posts_columns Existing array of columns displayed in the Media list table. * @return array Amended array of columns to be displayed in the Media list table. */ function codfe_media_columns_filesize( $posts_columns ) { $posts_columns['filesize'] = __( 'File Size', 'my-theme-text-domain' ); return $posts_columns; } add_action( 'manage_media_custom_column', 'codfe_media_custom_column_filesize', 10, 2 ); /** * Display File Size custom column in the Media list table. * * @param string $column_name Name of the custom column. * @param int $post_id Current Attachment ID. */ function codfe_media_custom_column_filesize( $column_name, $post_id ) { if ( 'filesize' !== $column_name ) { return; } $bytes = filesize( get_attached_file( $post_id ) ); echo size_format( $bytes, 2 ); } add_action( 'admin_print_styles-upload.php', 'codfe_filesize_column_filesize' ); /** * Adjust File Size column on Media Library page in WP admin */ function codfe_filesize_column_filesize() { echo '<style> .fixed .column-filesize { width: 10%; } </style>'; } // Make column sortable function codfe_add_column_sortable_file_size($columns) { $columns['filesize'] = 'filesize'; return $columns; } add_filter('manage_upload_sortable_columns', 'codfe_add_column_sortable_file_size'); // Column sorting logic (query modification) function codfe_sortable_file_size_sorting_logic($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' == $_REQUEST['order']) { $query->set('order', 'DSC'); } } } add_action('pre_get_posts', 'codfe_sortable_file_size_sorting_logic'); |
Thay my-theme-text-domain bằng domain theme của bạn nhé (sẽ dùng cho dịch theme)