Opened 2 years ago

#16635 new enhancement

Handle grouped MIME-Types

Reported by: sergiu.paraschiv Owned by:
Priority: normal Milestone: Awaiting Review
Component: Media Version: 3.1
Severity: minor Keywords: has-patch
Cc:

Description

I'm trying to add a filter in the Media Library for common archive file types. The problem is that there are a lot of MIME-Types for archive files and none have a common prefix. Wildcards are not a solution here.

POC:

add_filter('post_mime_types', 'modify_post_mime_types');

function modify_post_mime_types($post_mime_types) {
	$type = 'application/zip,application/x-tar';
	$labels = array(
		__('Archive'), 
		__('Manage Archives'), 
		__ngettext_noop('Archive <span class="count">(%s)</span>', 'Archives <span class="count">(%s)</span>')
	);

	$post_mime_types[$type] = $labels;

	return $post_mime_types;
}

I've tried using "|" (regexp) as a separator:

$type = 'application/zip|application/x-tar';

This works ok in wp_match_mime_types but fails in wp_post_mime_type_where.

I've found two possible solutions:

1) Change

$post_mime_types = array_map('trim', explode(',', $post_mime_types));

to

$post_mime_types = array_map('trim', preg_split('/,|\|/', $post_mime_types));

in wp_post_mime_type_where and use "|" as a separator;

2) Handle this in wp_match_mime_type (see code in attached file) and use "," as a separator.

I'm aware that this is not a widely used filter but in my case this enhancement makes a lot of sense. If it works for images why shouldn't it work for archives? Also, why is wp_post_mime_type_where designed for comma-separated lists of MIME-Types if it's never used that way?

Attachments (1)

wp_match_mime_types.txt (1.4 KB) - added by sergiu.paraschiv 2 years ago.
New wp_match_mime_types definition.

Download all attachments as: .zip

Change History (1)

New wp_match_mime_types definition.

Note: See TracTickets for help on using tickets.