0

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 ^^

2 Answers 2

1

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.

1

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

Your Answer

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

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