WordPress Media Manager – limit to specific mime type

This is not full solution, it’s only a direction for you to work with media uploader

Firstly, you need to know how to implement Media Uploader in your plugin/theme. You can learn that from the file: wp-admin/js/custom-background.js which is used by WP for custom background page.

If that’s too hard to understand, then read this tutorial, it explains each line of code for you.

Secondly, when you implement Media Uploader, you know there’s an option object for the media frame. That object contains a list of many options, which are non-documented. We can see them only by viewing the wp-includes/js/media-views.js file.

For your need, you need to pass an option type for library, some thing like this:

// Create a frame only if needed
if ( !frame )
{
    var frameOptions = ( {
        className   : 'media-frame rwmb-file-frame',
        multiple    : true,
        title       : 'Select files'
    } );

    if ( mimeType )
    {
        frameOptions.library = {
            type : mimeType
        };
    }

    frame = wp.media( frameOptions );
}

// Open media uploader
frame.open();

// Remove all attached 'select' event
frame.off( 'select' );

// Handle selection
frame.on( 'select', function() {... } );

There’s variable in the code: mimeType, and you need to pass value to it using jQuery when user choose a type from your 1st input field.

Leave a Comment