Skip to content

Instantly share code, notes, and snippets.

@norcross
Last active January 4, 2016 10:19
Show Gist options
  • Save norcross/8608061 to your computer and use it in GitHub Desktop.
Save norcross/8608061 to your computer and use it in GitHub Desktop.
the uploader
<?php
echo '<tr class="addon-file-field">';
echo '<th>';
echo '<label for="addon-file">'.__( 'ZIP file', '' ).'</label>';
echo '</th>';
echo '<td>';
echo '<input type="text" name="addon-meta[file]" id="addon-file" class="widefat" value="'.esc_url( $file ).'">';
echo '</td>';
echo '</tr>';
echo '<tr class="addon-file-button">';
echo '<th>&nbsp;</th>';
echo '<td>';
echo '<input data-uploader_title="'.__( 'Select A File', '' ).'" data-uploader_button="'.__( 'Select', '' ).'" class="button button-secondary addon-file-upload" type="button" value="'.__( 'Add File', '' ).'">';
echo '</td>';
echo '</tr>';
//********************************************************
// now start the engine
//********************************************************
jQuery(document).ready( function($) {
//********************************************************
// media uploader for 3.5
//********************************************************
// Uploading files
var file_frame;
jQuery( 'tr.addon-file-button' ).on('click', 'input.addon-file-upload', function( event ){
event.preventDefault();
//get my field ID for later
var fieldbox = jQuery( 'div#add-on-file' ).find( 'input#addon-file' );
// If the media frame already exists, reopen it.
if ( file_frame ) {
file_frame.open();
return;
}
// 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' )
},
multiple: false
});
// When an image is selected, run a callback.
file_frame.on( 'select', function() {
// We set multiple to false so only get one item from the uploader
attachment = file_frame.state().get('selection').first().toJSON();
// clear the existing value
jQuery( fieldbox ).val( '' );
// Populate the field with the file URL
jQuery( fieldbox ).val( attachment.url );
});
// Finally, open the modal
file_frame.open();
});
//********************************************************
// that's all folks. we're done here
//********************************************************
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment