WordPress media popup script

<script type="text/javascript">
// Uploading files
var file_frame;
 
jQuery(document).on('click', '.upload_file_button', function (event) {
 
	event.preventDefault();
 
	// If the media frame already exists, reopen it.
	if (file_frame) {
		file_frame.open();
		return;
	}
 
	// Create the media frame.
	file_frame = wp.media.frames.downloadable_file = wp.media({
		title: 'Choose file',
		button: {
			text: 'Use file'
		},
		multiple: false
	});
 
	// When an image is selected, run a callback.
	file_frame.on('select', function () {
		var attachment = file_frame.state().get('selection').first().toJSON();
		console.log(attachment);
		jQuery('#input_audio').val(attachment.url);
		jQuery('.sm-preview').html('Listen file').attr('href',attachment.url);
 
	});
 
	// Finally, open the modal.
	file_frame.open();
});
</script>
<button type="submit" class="upload_file_button button">Upload</button>
<a class="sm-preview">Audio file not set.</a>
<input type="hidden" name="input_audio" id="input_audio" value=""/>

Leave a Reply

Your email address will not be published. Required fields are marked *