jQuery(document).ready(function(){ if( typeof wp != "undefined" ){ // Uploading files var wp_media_post_id = wp.media.model.settings.post.id; // Store the old id jQuery(document).on( 'click', '.wck_upload_button', function( event ){ event.preventDefault(); var set_to_post_id = jQuery( this ).data( 'post_id' ); // Set this var file_frame; var uploadInputId = jQuery( this ).data( 'upload_input' ); var uploadInFront = jQuery( this ).data( 'upload_in_backend' ); var attachToPost = jQuery( this ).data( 'attach_to_post' ); var uploadButton = jQuery( this ); /* remove set_to_post_id value if we do not want to attach to post */ if( attachToPost != true ){ set_to_post_id = ''; } /* set default tab to upload file */ wp.media.controller.Library.prototype.defaults.contentUserSetting = false; if( uploadInFront != true ) wp.media.controller.Library.prototype.defaults.router = false; wp.media.controller.Library.prototype.defaults.searchable = true; wp.media.controller.Library.prototype.defaults.sortable = false; // If the media frame already exists, reopen it. if ( file_frame ) { // Set the post ID to what we want file_frame.uploader.uploader.param( 'post_id', set_to_post_id ); // Open frame file_frame.open(); return; } else { // Set the wp.media post id so the uploader grabs the ID we want when initialised wp.media.model.settings.post.id = set_to_post_id; } // Create the media frame. file_frame = wp.media.frames.file_frame = wp.media({ title: jQuery( this ).data( 'uploader_title' ), button: { text: jQuery( this ).data( 'uploader_button_text' ), }, multiple: jQuery( this ).data( 'multiple_upload' ) // Set to true to allow multiple files to be selected }); /* restrict allowed file types if we have to */ allowedTypes = jQuery( this ).data( 'allowed_types' ); if( allowedTypes != undefined ) file_frame.uploader.options.uploader['params']['allowed_type'] = allowedTypes; // When an image is selected, run a callback. file_frame.on( 'select', function() { // We set multiple to false so only get one image from the uploader attachments = file_frame.state().get('selection').toJSON(); var attids = []; for( var i=0;i < attachments.length; i++ ){ // Do something with attachment.id and/or attachment.url here attids.push( attachments[i].id ); result = '
'+attachments[i].filename+''+attachments[i].mime +'Remove