Tag Archives: media upload

media uploader script

functions.php


function legal_framework_load_scripts() {
wp_enqueue_media();
wp_enqueue_script('mymedia-uploader', get_template_directory_uri() . '/js/mymedia-uploader.js', array('jquery'));
}
add_action('admin_enqueue_scripts', 'legal_framework_load_scripts');

mymedia-uploader.js


jQuery(document).ready( function(){
function media_upload( button_class) {
var _custom_media = true,
_orig_send_attachment = wp.media.editor.send.attachment;
jQuery('body').on('click',button_class, function(e) {
var button_id ='#'+jQuery(this).attr('id');
/* console.log(button_id); */
var self = jQuery(button_id);
var send_attachment_bkp = wp.media.editor.send.attachment;
var button = jQuery(button_id);
var id = button.attr('id').replace('_button',"");
_custom_media = true;
wp.media.editor.send.attachment = function(props, attachment){
if ( _custom_media  ) {
jQuery('.mytheme_media_url').val(attachment.url);
} else {
return _orig_send_attachment.apply( button_id, [props, attachment] );
}
}
wp.media.editor.open(button);
return false;
});
}
media_upload( '.mytheme_media_upload');
});

%d bloggers like this: