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 some meta boxes of pages on the theme i'm working on.

The idea is to have a input field, then a button to upload files.

The meta box is working properly.

When I click on the button, it fires the thickbox.

This is working fine too.

Then I could upload a media or choose a media for the library.

A little bit of jQuery need to grab the URL of the chosen file in ThickBox and put it in the correct field.

I had a script which works fine with images, but it's not working on PDF files.

//lauch on click to appropriate button
jQuery('#upload_requestform_button').click(function() {

    // keep copy of the original send to editor
    original_send_to_editor = window.send_to_editor;


    //new send_to_editor function
    window.send_to_editor = function(html) {
    // i guess this where i'm wrong
    imgurl = jQuery('img',html).attr('src');
    // sending the data to the input
    jQuery('#box_requestform_meta').val(imgurl);
    tb_remove();
    // restore send_to_editor to post content 
    window.send_to_editor = original_send_to_editor;
   }
    // loads thickbox
    formfield = jQuery('#box_requestform_meta').attr('name');
    tb_show('', 'media-upload.php?type=image&TB_iframe=true');
    return false;
  });

and this is the HTML part :

<label for="ala_request_form_ID"><?php _e( "Request Form", 'Request Form' ); ?></label><br />
<input id="box_requestform_meta" type="text" size="80%" name="box_requestform_meta" value="<?php echo esc_attr( get_post_meta( $object->ID, 'box_requestform_meta', true ) ); ?>" />
<input id="upload_image_button" type="button" value="Upload Request Form" />

I couldn't find much documentation or examples about how to use thickbox, any help appreciated ^^

share|improve this question
add comment

3 Answers

This line...

imgurl = jQuery('img',html).attr('src');

Means that jQuery is getting the "source" (i.e. full path) of the image. As you've guessed, that won't work for a PDF file.

What you'll probably have to do is get the id of the attachment instead. Could you possibly update your original answer with the HTML SOURCE of the thickbox? We might be able to find the ID from somewhere and amend that jQuery line.

Once you've got the ID of the attachment, you can use the following code to display the URL in your HTML whether it's an image or any other type of attachment:

<?php wp_get_attachment_url( $id ); ?>

Hope that helps.

share|improve this answer
add comment

I found the way to do it (from a useful comment there http://www.webmaster-source.com/2010/01/08/using-the-wordpress-uploader-in-your-plugin-or-theme/)

//lauch on click to appropriate button
jQuery('#upload_requestform_button').click(function() {

// keep copy of the original send to editor
original_send_to_editor = window.send_to_editor;


//new send_to_editor function
window.send_to_editor = function(html) {
// just take the href attribute
hrefurl = jQuery(html).attr('href');
// sending the data to the input
jQuery('#box_requestform_meta').val(hrefurl);
tb_remove();
// restore send_to_editor to post content 
window.send_to_editor = original_send_to_editor;
}
// loads thickbox
formfield = jQuery('#box_requestform_meta').attr('name');
tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
return false;
});
share|improve this answer
add comment

This discussion touches on this as well.

Using media-upload.php to upload mp3 via custom fields

The person here is looking for mp3 files but the process should be very similar for PDF files. It boils down to that the uploader returns different code for images than other types of files.

share|improve this answer
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.