WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 20 months ago

#16635 closed enhancement (worksforme)

Handle grouped MIME-Types

Reported by: sergiu.paraschiv Owned by:
Milestone: Priority: normal
Severity: minor Version: 2.9
Component: Media Keywords: needs-patch close 2nd-opinion
Focuses: 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 4 years ago.
New wp_match_mime_types definition.

Download all attachments as: .zip

Change History (3)

@sergiu.paraschiv4 years ago

New wp_match_mime_types definition.

comment:1 @c3mdigital21 months ago

  • Keywords needs-patch close 2nd-opinion added; has-patch removed
  • Version changed from 3.1 to 2.9

comment:2 @nacin20 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed

So much has changed here in the intervening time. Please reopen if still relevant.

Note: See TracTickets for help on using tickets.