• Resolved David Innes

    (@david-innes)


    Totally impressed with the breadth and scope of this plugin and I’m likely to install it on all my client sites. So of course I’ve immediately got a feature request. (But, perhaps surprisingly, only one.)

    Could you make the image size column sortable in the media library? It would make tracking down those monster .PNGs that need to be replaced with appropriately optimized and sized JPGs or WebPs on older sites.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Try this code I’ve tested and it works 100%.

    /**
     * Make asenha-file-size sortable
     */
    function asenha_make_file_size_columns_sortable($sortable_columns) {
        $sortable_columns['asenha-file-size'] = 'asenha-file-size';
        return $sortable_columns;
    }
    add_filter('manage_upload_sortable_columns', 'asenha_make_file_size_columns_sortable');
    
    /**
     * Handle sorting asenha-file-size
     */
    function asenha_sort_media_by_file_size($query) {
        if (!is_admin() || !$query->is_main_query()) {
            return;
        }
    
        $orderby = $query->get('orderby');
    
        if ($orderby === 'file_size') {
            $query->set('meta_key', '_wp_attachment_filesize');
            $query->set('orderby', 'meta_value_num');
        }
    }
    add_action('pre_get_posts', 'asenha_sort_media_by_file_size');

    @tinnyfusion @david-innes I’ve made all the columns sortable and add 3 new column 1 accessibility stats, 1 seo stats & one to show for file EXT witch are also sortable as well granted the accessibility & seo status column are no perfect nor sortable yet

    // Register custom columns for media library
    function asenha_add_more_media_columns($columns) {
        $columns['asenha-media-ext'] = __('Type', 'textdomain');
        $columns['seo-status'] = __('SEO', 'textdomain');
        $columns['accessibility-status'] = __('Accessibility', 'textdomain');
        return $columns;
    }
    add_filter('manage_media_columns', 'asenha_add_more_media_columns');
    
    // Populate content for custom columns in media library
    function asenha_add_more_media_column_content($column_name, $attachment_id) {
        switch ($column_name) {
            case 'asenha-media-ext':
                // Display file extension in uppercase
                $file_url = wp_get_attachment_url($attachment_id);
                $filetype = wp_check_filetype($file_url);
                echo strtoupper($filetype['ext']);
                break;
    
            case 'seo-status':
                // Check SEO status (alt text and caption)
                $alt_text = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
                $caption = get_post_field('post_excerpt', $attachment_id);
    
                if (!empty($alt_text) && !empty($caption)) {
                    // Display green checkmark icon if both alt text and caption are set
                    echo '<span class="dashicons dashicons-yes" style="color: green;"></span>';
                } elseif (!empty($alt_text) && empty($caption)) {
                    // Display yellow checkmark icon if either alt text or caption is set
                    echo '<span class="dashicons dashicons-yes" style="color: yellow;"></span>';
                } else {
                    // Display red x icon if neither alt text nor caption is set
                    echo '<span class="dashicons dashicons-no" style="color: red;"></span>';
                }
                break;
    
            case 'accessibility-status':
                // Check accessibility status (alt text)
                $alt_text = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
            
                if (!empty($alt_text)) {
                    // Display green checkmark icon if alt text is set
                    echo '<span class="dashicons dashicons-yes" style="color: green;"></span>';
                } else {
                    // Display red x icon if alt text is not set
                    echo '<span class="dashicons dashicons-no" style="color: red;"></span>';
                }
                break;
    
            default:
                // Handle default case (if column name doesn't match any of the above)
                echo '';
                break;
        }
    }
    add_action('manage_media_custom_column', 'asenha_add_more_media_column_content', 10, 2);
    
    /**
     * Make asenha-file-size sortable
     */
    function asenha_make_file_size_columns_sortable($sortable_columns) {
        $sortable_columns['asenha-file-size'] = 'asenha-file-size';
        $sortable_columns['asenha-media-ext'] = 'asenha-media-ext';
        $sortable_columns['asenha-id'] = 'asenha-id';
        return $sortable_columns;
    }
    add_filter('manage_upload_sortable_columns', 'asenha_make_file_size_columns_sortable');
    
    /**
     * Handle sorting asenha-file-size
     */
    function asenha_sort_media_by_file_size($query) {
        if (!is_admin() || !$query->is_main_query()) {
            return;
        }
    
        $orderby = $query->get('orderby');
    
        if ($orderby === 'file_size') {
            $query->set('meta_key', '_wp_attachment_filesize');
            $query->set('orderby', 'meta_value_num');
        }
    
        if ($orderby === 'id') {
            $query->set('meta_key', '_wp_attachment_id');
            $query->set('orderby', 'meta_value_num');
        }
    
        if ($orderby === 'ext') {
            $query->set('meta_key', '_wp_attachment_ext');
            $query->set('orderby', 'meta_value_text');
        }
    }
    add_action('pre_get_posts', 'asenha_sort_media_by_file_size');
    Plugin Author Bowo

    (@qriouslad)

    @pressthemes1 thanks for sharing the snippet. Will test for possible inclusion in the next release.

    @tinnyfusion media categories is part of the Pro version.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Feature request: Option to sort media by file size?’ is closed to new replies.