5

I'm using the following code to add new sorting options to my Media Library:

function modify_post_mime_types( $post_mime_types ) {

    $post_mime_types['application/pdf'] = array( __( 'PDFs' ), __( 'Manage PDFs' ), _n_noop( 'PDF <span class="count">(%s)</span>', 'PDFs <span class="count">(%s)</span>' ) );
    $post_mime_types['application/vnd.openxmlformats-officedocument.presentationml.presentation'] = array( __( 'Powerpoints' ), __( 'Manage Powerpoints' ), _n_noop( 'Powerpoints <span class="count">(%s)</span>', 'Powerpoints <span class="count">(%s)</span>' ) );
    $post_mime_types['application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document'] = array( __( 'Documents' ), __( 'Manage Documents' ), _n_noop( 'Documents <span class="count">(%s)</span>', 'Documents <span class="count">(%s)</span>' ) );

    return $post_mime_types;

}

add_filter( 'post_mime_types', 'modify_post_mime_types' );

Which appears to be working great for the JavaScript-enabled "Grid" mode:

PDFs Grid

However, in the "List" mode, clicking on the entries shows all the extensions/files (even though it is counting them properly) as shown:

PDFs List

Here's the URL it is redirecting to for the PDFs:

wp-admin/upload.php?mode=list&attachment-filter=post_mime_type%3Aapplication%252Fpdf&m=0&filter_action=Filter&s&action=-1&paged=1&action2=-1&affected&_ajax_nonce=093720f202&ps

If I try with the standard Wordpress "Images" filter, it indeed works. Here's the URL it gives for that:

wp-admin/upload.php?mode=list&attachment-filter=post_mime_type%3Aimage&m=0&filter_action=Filter&s&action=-1&paged=1&action2=-1&affected&_ajax_nonce=093720f202&ps

I'm thinking this is perhaps a Wordpress bug, but just making sure if anybody knows anything here.

2
  • 1
    I have a decent workaround here: wordpress.stackexchange.com/a/170471/34438
    – kitchin
    Dec 7, 2014 at 1:43
  • @kitchin Sorry, I can't comment on that yet, so hopefully you get this. For some reason it doesn't look like it is getting all the files. Just as an example, it says PDFs (10), but when I filter, it's only returning the same 4 files. The "Grid" mode returns the proper amount. It doesn't seem to be a file naming issue or anything like that.
    – Drath
    Dec 9, 2014 at 17:28

1 Answer 1

1

The problem lies in the attachment-filter= URL parameter. The slash between application/pdf (or else) is being escaped twice. When you replace the %252F with a slash or %2F the filter will work.

The first escaping (/ becomes %2F) happens when WP is building the selectbox containing the mime types. The second one (%2F becomes %252F) is performed by the browser.

The easiest Solution would be to manually urldecode the URL param somewhere in PHP:

if ( isset( $_GET['attachment-filter'] ) )
    $_GET['attachment-filter'] = urldecode($_GET['attachment-filter']);

Update: There is a WP core ticket on that issue: https://core.trac.wordpress.org/ticket/30123

Once this gets fixed, the answer above will be obsolete.

2
  • Do you know where this code could be implemented? Just as a quick hack I added it to functions.php and wp-admin/index.php with no success.
    – Drath
    Nov 14, 2014 at 18:26
  • The code needs to be executed in the backend. Neither of wp-admin/index.php and functions.php is. It chould live either in a plugin or in a mu-plugin. Inside the modify_post_mime_types function above might also be a good place.
    – Jörn Lund
    Nov 15, 2014 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.