Custom post type with file upload – need to “set as field” instead of “send to editor”

I’ve created a custom post type. It requires a file to be uploaded. I found the following tutorial which I’ve successfully adapted to do what I need with one exception:

http://www.webmaster-source.com/2010/01/08/using-the-wordpress-uploader-in-your-plugin-or-theme/

Read More

The tutorial provides JavaScript code to override the “send to editor” button, however that’s not what I want to do. I want to set the filename as a meta value of the post so I can pull it out in my display template. Ideally I want to display another custom link in the file upload thickbox, similar to “set as post thumbnail”, which will do this.

My custom JavaScript file contains this (the first function has been customised to my post type, the second hasn’t):

jQuery(document).ready(function() {

jQuery('#upload_resource_button').click(function() {
 formfield = jQuery('#upload_resource').attr('name');
 tb_show('', 'media-upload.php?type=file&TB_iframe=true');
 return false;
});

window.send_to_editor = function(html) {
 imgurl = jQuery('img',html).attr('src');
 jQuery('#upload_image').val(imgurl);
 tb_remove();
}

});

If I need an additional hidden field to assign the filename back to, I can probably work that out – really what I need to know is what hook to use to add a new link to the thickbox to use instead of “Send To Editor”.

I hope that makes sense – I’m not sure of all the correct terminology.

Thanks!

Related posts

Leave a Reply

1 comment

  1. i cant say I’ve tried this but if you have the url of the file and all you need is the file name then you can just change your code from this:

        window.send_to_editor = function(html) {
          imgurl = jQuery('img',html).attr('src');
          jQuery('#upload_image').val(imgurl);
          tb_remove();
        }
    

    to this:

        window.send_to_editor = function(html) {
          imgurl = jQuery('img',html).attr('src');
          filename = substring(imgurl.lastIndexOf('/'), imgurl.length);
          jQuery('#upload_resource').val(filename);
          tb_remove();
        }
    

    and this way your “send to editor” button will insert just the file name.