WordPress 3.5 Media Uploader – Only allow 1 upload and certain file types

I am trying to include the new WP Media Uploader in a Custom Meta Box.

I found this tutorial.

Read More

However, I am looking for a way to limit the uploader to only accept certain types of files like ‘application/msword’, ‘application/vnd.ms-excel’, ‘application/pdf’, etc. and to return the linked document url to a text field i have in the metabox

Any ideas, links or just basically anything to point me in the right direction would be highly appreciated!

Related posts

Leave a Reply

2 comments

  1. The file types that you have specified ‘application/msword’, ‘application/vnd.ms-excel’, ‘application/pdf’ are already supported by media uploader.

    To see the default supported mime file types, call wp_get_mime_types() function.

    Use upload_mimes filter as shown in following code to make media uploader to accept files types other than the default.

    Add following code in your themes functions.php file

    // Add the filter
    add_filter('upload_mimes', 'custom_upload_mimes');
    
    function custom_upload_mimes ( $existing_mimes=array() ) {
    
    // Add file extension 'extension' with mime type 'mime/type'
    $existing_mimes['prc'] = 'application/x-mobipocket'; 
    
    // and return the new full result
    return $existing_mimes; 
    }
    
  2. Like Vinod Dalvi said, you could use the filter upload_mimes to achieve what you need.
    You can deregister existing mimes types using

    unset($mime_types['pdf']);
    

    and adding new type by adding then to the array :

    $mime_types['avi'] = 'video/avi';
    

    for instance

    function my_myme_types($mime_types){
        $mime_types['avi'] = 'video/avi'; //Adding avi extension
        unset($mime_types['pdf']); //Removing the pdf extension
        return $mime_types;
    }
    add_filter('upload_mimes', 'my_myme_types', 1, 1);
    

    This is taken from here

    You say :

    how do i limit the mime types to only the selected mime types for that
    specific instance of the media upload? I don’t want to limit the
    standard WP Editor upload to those same mime types

    Do you mean limiting mime type only for one type of uploader ? I can’t see the logic involved, since if there are other “instances” of media uploader, “forbidden” media types could be uploaded anyway.