0

i would like to use the brand new wp's upload tool in a personnal theme options page. I found a lot of tutorials like : wp media uploader in plugins ...and it works very fine. But i'd like to filter the file type the end user can select ( and view ). I found many examples that use this way to filter by 'image' type ( param for wp.media object ) :

library:{ type:'image' }

You can see an example here. It works, when the uploader panel appear you only see pictures BUT :

  • How can i filter other file types ( documents, zips, videos... ), i tried to replace 'image' by 'video' or 'document' it doesn't work
  • When the end user choose to upload a file he can upload any file type instead of an image

How can i fixed that ? How to set a filter for one or more specific file types ?

Thanks a lot for help.

2

The library parameter is actually responsible of what you see in the library frame and not what you can upload. It accepts : image,audio,video,file or any other mime type for example to show only pdf's :

library: {
    type: 'application/pdf'
},

Now to actually limit the upload to a file type you need to add a parameter to the uploader and catch that parameter using wp_handle_upload_prefilter filter hook.

To add a parameter use:

file_frame.uploader.uploader.param( 'allowed_Type', 'pdf');

and to filter the file type use

add_filter('wp_handle_upload_prefilter', 'Validate_upload_file_type');
function Validate_upload_file_type($file) {
    if (isset($_POST['allowed_Type']) && !empty($_POST['allowed_Type'])){
        //this allows you to set multiple types seperated by a pipe "|"
        $allowed = explode("|", $_POST['allowed_Type']);

        $ext =  substr(strrchr($file['name'],'.'),1);
        //first check if the user uploaded the right type
        if (!in_array($ext, (array)$allowed)){
            $file['error'] = __("Sorry, you cannot upload this file type for this field.");
            return $file;
        }
        //check if the type is allowed at all by WordPress
        foreach (get_allowed_mime_types() as $key => $value) {
            if (strpos($key, $ext) || $key == $ext)
                return $file;
        }
        $file['error'] = __("Sorry, you cannot upload this file type for this field.");
    }
    return $file;
}
2

Just a slight modification to Bainternet's answer. I'm using WP 3.7.1 and something must have changed in Media Upload since this line does not seem to work for me:

file_frame.uploader.uploader.param( 'allowed_Type', 'pdf');

The following error is shown in Firebug console when opening the Media Upload:

Uncaught TypeError: Cannot call method 'param' of undefined
(anonymous function)
x.event.dispatch
v.handle

This worked for me instead:

file_frame.uploader.options.uploader['params']['allowed_type'] = 'pdf';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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