Skip to content

Instantly share code, notes, and snippets.

@mauryaratan
Created December 14, 2012 06:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mauryaratan/4283059 to your computer and use it in GitHub Desktop.
Save mauryaratan/4283059 to your computer and use it in GitHub Desktop.
WordPress 3.5 media uploader in easier steps. If using other than edit post screen, don't forget to use wp_enqueue_media(); in your functions.php
jQuery(document).ready(function($){
var _custom_media = true,
_orig_send_attachment = wp.media.editor.send.attachment;
$('.stag-metabox-table .button').click(function(e) {
var send_attachment_bkp = wp.media.editor.send.attachment;
var button = $(this);
var id = button.attr('id').replace('_button', '');
_custom_media = true;
wp.media.editor.send.attachment = function(props, attachment){
if ( _custom_media ) {
$("#"+id).val(attachment.url);
} else {
return _orig_send_attachment.apply( this, [props, attachment] );
};
}
wp.media.editor.open(button);
return false;
});
$('.add_media').on('click', function(){
_custom_media = false;
});
});
@hemant-tivlabs
Copy link

hemant-tivlabs commented Jun 2, 2017

Hi, this solution was really helpful. But when I used it with Wordpress v4.7.5, everything worked fine but I would also receive a:

TypeError: document.getElementById(...) is null

With some experimenting, I got rid of the error. Please check updated code here.

@meetsohail
Copy link

meetsohail commented Mar 13, 2019

How do we get all the selected URLs? Currently, we only receive only one file from selected files in variable attachment.url.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment