Skip to content

Instantly share code, notes, and snippets.

@kosmiq
Created September 23, 2015 19:49
Show Gist options
  • Save kosmiq/79252f5bb310975391bb to your computer and use it in GitHub Desktop.
Save kosmiq/79252f5bb310975391bb to your computer and use it in GitHub Desktop.
jQuery(document).ready(function($) {
var formfield = null;
$('#upload_image_button').click(function() {
$('html').addClass('Image');
formfield = $(this).prev('input').attr('name');
formfield_id = $(this).prev('input').attr('id');
tb_show( '', 'media-upload.php?type=image&TB_iframe=true' );
return false;
});
// user inserts file into post.
// only run custom if user started process using the above process
// window.send_to_editor(html) is how wp normally handles the received data
window.original_send_to_editor = window.send_to_editor;
window.send_to_editor = function( html ) {
var fileurl;
if(formfield != null) {
fileurl = $( 'img', html).attr('src');
$( "#" + formfield_id ).val(fileurl);
tb_remove();
$('html').removeClass('Image');
formfield = null;
} else {
window.original_send_to_editor(html);
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment