2

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/

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!

1 Answer 1

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.

4
  • I was hoping to replace the "send to editor" button with something that made more sense to the end users but I guess it's not really all that important in the scheme of things - "Send to editor" is exactly what we're doing. So thanks, that seems to do what I need :)
    – goatlady
    Jan 17, 2011 at 15:05
  • Glad i could help
    – Bainternet
    Jan 17, 2011 at 15:37
  • 1
    is there a way to get the id instead of the src or filename? Would be useful for using different thumbnail sizes!
    – v3nt
    Aug 10, 2011 at 8:31
  • var id = $('img', html).attr('class').replace(/(.*?)wp-image-/, '');
    – Jake
    Aug 13, 2012 at 0:17

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.