2

I have created a custom post type called "pdf". I've have a custom metabox where user can upload a pdf file throught wordpress builtin media uploader I need to restrict allowed files (pdf) only in this uploader. I already know how to restrict allowed files: How to set file type in wp_handle_upload? but the link above, interfer with all the uploads. Any hint?

Thanks

Edit: I've found how to limit file type in plupload, so I've solved half of the problem:

 add_filter( 'plupload_init', 'my_plupload_init', 0, 1 );

 function my_plupload_init( $plupload_init ) {
 $plupload_init['filters'] = array( array('title' => __( 'Allowed Files' ), 'extensions' => 'pdf') );
 return $plupload_init;
}

Edit #2: Use of wp_handle_upload_prefilter filter doesn't work: Thi is what is available inside $_REQUEST object after an image is uploaded on a new post:

    array
  'type' => string 'image' (length=5)
  'tab' => string 'type' (length=4)
  'post_id' => string '0' (length=1)
  '_wpnonce' => string '375c5f46f2' (length=10)
  '_wp_http_referer' => string '/wp-admin/media-upload.php?post_id=' (length=37)
  'html-upload' => string 'Carica media' (length=12)

There are no info about my custom post type. The id is 0 because post is not saved yet.

Edit #3:

Thanks to @TheDeadMusic answer I've added one line of code to my javascript. This is the js code I actually use that works great:

jQuery(document).ready(function($){
// #btn_upload is my custom button
$('#btn_upload').click(function(event){
    event.preventDefault();
    var backup = window.send_to_editor;
    var src=''; 
//post_id is always set so I can pass it safely to tickbox
    var post_id=$('#post_ID').val(); 
var target=$('#pdf_url');
    window.send_to_editor = function(html) 
     {
//I have to get the link to "original file" from html returned by tickbox
        link = $('a',html).attr('href');
         $(target).val(link);
         tb_remove();
         window.send_to_editor=backup; //reset default action
    }
    //show the uploader
     tb_show('', 'media-upload.php?post_id='+post_id+'&TB_iframe=true');

}); 

});
2
  • So how can the upload get attached if there's no post ID? WP automatically creates & saves a new post (an auto-draft) whenever you load post-new.php; that's how the default media uploader works, even before you save/publish. What's the code that's init'ing the uploader in your metabox? Commented Jul 25, 2012 at 14:44
  • @TheDeadMedic I didn't know that. Your last answer was the key. I didn't pass post_id because I didn't know that post id is generated immediately. Thanks!
    – cardy
    Commented Aug 2, 2012 at 10:48

1 Answer 1

5
function wpse_59621_mimes_filter( $mimes ) {
    return array( 'pdf' => 'application/pdf' );
}

function wpse_59621_delay_mimes_filter( $value ) {
    if ( isset( $_REQUEST['post_id'] ) && get_post_type( $_REQUEST['post_id'] ) === 'my_post_type' )
        add_filter( 'upload_mimes', 'wpse_59621_mimes_filter' );
    else
        remove_filter( 'upload_mimes', 'wpse_59621_mimes_filter' );

    return $value;
}

add_filter( 'wp_handle_upload_prefilter', 'wpse_59621_delay_mimes_filter' );

Let us know how it goes - this is untested, but I'm confident!

2
  • +1 for »this is untested, but I'm confident!«. Wonder why you never join us in chat? :)
    – kaiser
    Commented Jul 25, 2012 at 14:12
  • small hint: See this repo for an solution, customize it for your requirement. Maybe it's usefull for you.
    – bueltge
    Commented Aug 3, 2012 at 21:25

Your Answer

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

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