Open WordPress Media Window
This opens WordPress’ Media Library in the backend
var custom_uploader;
$(document).on("click", '.xupload_image_button', function (e) { //1AM
e.preventDefault();
var $upload_button = $(this);
var this_textarea = $( this ).siblings( ".pdf_list" );
var current_val = $( this ).siblings( ".pdf_list" ).val();
//Extend the wp.media object
custom_uploader = wp.media.frames.file_frame = wp.media({
title: 'Choose Image',
button: {
text: 'Choose Image'
},
multiple: false
});
//When a file is selected, grab the URL and set it as the text field's value
custom_uploader.on('select', function() {
var attachment = custom_uploader.state().get('selection').first().toJSON();
if (current_val == "") {
var new_val = attachment.url;
}else{
var new_val = current_val + '\n' + attachment.url;
}
this_textarea.val(new_val);
});
//Open the uploader dialog
custom_uploader.open();
});