WordPress media library attachment-filter: Different set of mime types are appearing in List and Grid view modes

I have an array of mime_types like the following:

$mime_map = array(
    /** Data types */
    array(
        'name' = 'Comma separated (csv)',
        'mime' = 'text/csv',
        'ext'  = 'csv',
    ),
    array(
        'name' = 'Spreadsheet',
        'mime' = 'application/excel',
        'ext'  = 'xl',
    ),
    array(
        'name' = 'Microsoft Excel',
        'mime' = 'application/vnd.ms-excel',
        'ext'  = 'xlsx',
    ),
    array(
        'name' = 'Microsoft Excel (xls)',
        'mime' = 'application/x-xls',
        'ext'  = 'xls',
    ),
    array(
        'name' = 'Spreadsheet (odf)',
        'mime' = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        'ext'  = 'odf',
    ),
    
    /** Image types */
    array(
        'name' = 'Image (gif)',
        'mime' = 'image/gif',
        'ext'  = 'gif',
    ),
    array(
        'name' = 'Image (png)',
        'mime' = 'image/png',
        'ext'  = 'png',
    ),
    array(
        'name' = 'Image (jpg)',
        'mime' = 'image/jpeg',
        'ext'  = 'jpg',
    ),
    array(
        'name' = 'Image (jpeg)',
        'mime' = 'image/pjpeg',
        'ext'  = 'jpeg',
    ),
    array(
        'name' = 'Image (svg)',
        'mime' = 'image/svg+xml',
        'ext'  = 'svg',
    ),
    array(
        'name' = 'Image (WebP)',
        'mime' = 'image/webp',
        'ext'  = 'webp',
    ),
    
    /** Video types */
    array(
        'name' = 'Video (3gp)',
        'mime' = 'video/3gp',
        'ext'  = '3gp',
    ),
    array(
        'name' = 'Video (3gpp)',
        'mime' = 'video/3gpp',
        'ext'  = '3gpp',
    ),
    array(
        'name' = 'Video (avi)',
        'mime' = 'video/avi',
        'ext'  = 'avi',
    ),
    array(
        'name' = 'Video (mp4)',
        'mime' = 'video/mp4',
        'ext'  = 'mp4v',
    ),
    
    /** Audio types */
    array(
        'name' = 'Audio (mp4)',
        'mime' = 'audio/mp4',
        'ext'  = 'mp4',
    ),
    array(
        'name' = 'Audio (mpeg)',
        'mime' = 'audio/mpeg',
        'ext'  = 'm4a',
    ),
    array(
        'name' = 'Audio (mp3)',
        'mime' = 'audio/mp3',
        'ext'  = 'mp3',
    ),
    
    /** Document types */
    array(
        'name' = 'Photoshop Document',
        'mime' = 'application/octet-stream',
        'ext'  = 'psd',
    ),
    array(
        'name' = 'PDF Document',
        'mime' = 'application/pdf',
        'ext'  = 'pdf',
    ),
    array(
        'name' = 'Office Word',
        'mime' = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        'ext'  = 'docx',
    ),
    array(
        'name' = 'MS Word Document',
        'mime' = 'application/msword',
        'ext'  = 'doc',
    ),
    array(
        'name' = 'MS PowerPoint',
        'mime' = 'application/vnd.ms-powerpoint',
        'ext'  = 'ppt',
    ),
    array(
        'name' = 'Office PowerPoint',
        'mime' = 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
        'ext'  = 'pptx',
    ),
    array(
        'name' = 'Document (odt)',
        'mime' = 'application/vnd.oasis.opendocument.text',
        'ext'  = 'odt',
    ),
    
    /** Text types */
    array(
        'name' = 'Text (Plain)',
        'mime' = 'text/plain',
        'ext'  = 'txt',
    ),
    array(
        'name' = 'Text (RTF)',
        'mime' = 'text/rtf',
        'ext'  = 'rtf',
    ),
    
    /** Archive types */
    array(
        'name' = 'Archive (gzip)',
        'mime' = 'application/x-gzip',
        'ext'  = 'gz',
    ),
    array(
        'name' = 'Archive (7zip)',
        'mime' = 'application/x-7z-compressed',
        'ext'  = '7zip',
    ),
    array(
        'name' = 'Archive (zip)',
        'mime' = 'application/zip',
        'ext'  = 'zip',
    ),
    array(
        'name' = 'Archive (rar)',
        'mime' = 'application/rar',
        'ext'  = 'rar',
    ),
    array(
        'name' = 'Archive (tar)',
        'mime' = 'application/x-tar',
        'ext'  = 'tar',
    ),
);

I am using the above array to populate attachment-filter dropdown with specific file types for better filtration. The filter works well in both list and grid view modes but the list of mime-types that appear in the said modes are different!

In list view, correct mime-types are appearing, i.e. only those which have at least one media was uploaded to. But in grid view, all available mime-types in the above array are appearing.

add_filter( 'post_mime_types', array( $this, 'wpmf_custom_mime_types' ), 10, 1 );
function wpmf_custom_mime_types( $post_mime_types ) {
    $media_mime_types       = new Media_Filter_Mime_Types();
    $types                  = $media_mime_types-media_types;
    
    foreach( $types as $key = $mime ) {
        $post_mime_types[$mime['mime']] = array( __( $mime['name'], 'text-domain' ) );
    }
    
    return $post_mime_types;
}

Screenshot (List view mode)

Screenshot (Grid view mode: more items are appearing)

Update #1 I added the following in the array

...
array(
    'name' = 'How about this',
    'mime' = 'text/hath',
    'ext'  = 'hat',
),
...

I tried to upload a file (how-about-this.hat), which as expected, failed to upload and this mime-type is not visible in the filter for list view either. But it does appear in grid view mode.

Update #2 I emptied the function body to look like this:

function wpmf_custom_mime_types( $post_mime_types ) {
    // Nothing here
}

And, as expected, the attachment-filter in both view modes become synchronized. But whenever I am putting the code back in the function body, the said problem starts to appear.

Is there anything I am missing or something specific is required to be done to synchronize the items because I am adding them through code?

Topic mime-types media-library Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.