2

I have a custom meta filed with upload file functionality but the problem is I can't get the fileurl button value when hit "insert into Post" below are my jQuery code

    window.send_to_editor = function(html) 

    {
        dlink = jQuery('button.urlfile',html).attr('title');
        jQuery('#download_link').val(dlink);
        tb_remove();
    }


    tb_show('', 'media-upload.php?post_id=1&type=image&TB_iframe=true');
    return false;
2
  • If you are uploading a file and not an image, it might be good to change "&type=image" to "&type=file" as a starting place.
    – Jake
    Aug 12, 2012 at 19:55
  • See this question, hints and source examples to the media uploader in wp 3.5.
    – bueltge
    Jan 20, 2013 at 11:46

3 Answers 3

2

Again. If you are using file upload, it is different than image upload. To get a FILE url, use: $(html).attr('href');

File upload:

window.send_to_editor = function(html){
    var file_url = $(html).attr('href');

    // Do something with those variables

    tb_remove();
};

Image upload:

window.send_to_editor = function(html){
    var file_url = $('img', html).attr('src'),
        classes  = $('img', html).attr('class'),
        id       = classes.replace(/(.*?)wp-image-/, '');

    // Do something with those variables

    tb_remove();
};
0

try this:

window.send_to_editor = function(html){
    dlink = jQuery('img',html).attr('src');
    jQuery('#download_link').val(dlink);
    tb_remove();
}
1
  • thanks for your help but I tried it out before and doesnt work since I upload is file not image :( Mar 24, 2012 at 3:26
0
<div>
    <label style="float:left; margin: 5px 5px 0 0;">Image 4:</label>
    <input type="text" id="image_4" name="image_4" value="" style="width: 550px; float:left; margin:0 5px;"/>
    <input id="_btn" class="upload_image_button" type="button" value="Upload Image" />
    <input type="hidden" name="image4_id" id="image4_id" value="<?php echo $image_id4; ?>" />
</div>

<script>
jQuery(document).ready(function () {
    var formfield;
    var id;
    jQuery('.upload_image_button').click(function () {
        jQuery('html').addClass('Image');
        formfield = jQuery(this).prev().attr('name');
        id = jQuery(this).next().attr('name');

        tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
        return false;
    });
    window.original_send_to_editor = window.send_to_editor;
    window.send_to_editor = function (html) {
        if (formfield) {
            fileurl = jQuery('img', html).attr('src');
            jQuery('#' + formfield).val(fileurl);

            imgclass = jQuery('img', html).attr('class');
            imgid = parseInt(imgclass.replace(/\D/g, ''), 10);
            jQuery('#' + id).val(imgid);

            tb_remove();
            jQuery('html').removeClass('Image');
        } else {
            window.original_send_to_editor(html);
        }
    };
});
</script>

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.