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'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!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
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 '11 at 15:05
    
Glad i could help –  Bainternet Jan 17 '11 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! –  daniel Crabbe Aug 10 '11 at 8:31
    
var id = $('img', html).attr('class').replace(/(.*?)wp-image-/, ''); –  Jake Aug 13 '12 at 0:17

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.