Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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');

}); 

});
share|improve this question
    
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? –  TheDeadMedic Jul 25 '12 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 Aug 2 '12 at 10:48
add comment

1 Answer

up vote 3 down vote accepted
add_filter( 'wp_handle_upload_prefilter', 'wpse_59621_delay_mimes_filter' );
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;
}

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

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

share|improve this answer
    
+1 for »this is untested, but I'm confident!«. Wonder why you never join us in chat? :) –  kaiser Jul 25 '12 at 14:12
    
@TheDeadMedic your solution doesn't work. I've updated my question with some details about your reply. Thx. –  cardy Jul 25 '12 at 14:21
    
small hint: See this repo for an solution, customize it for your requirement. Maybe it's usefull for you. –  bueltge Aug 3 '12 at 21:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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