Skip to content

Instantly share code, notes, and snippets.

@dbspringer
Last active August 29, 2015 14:20
Show Gist options
  • Save dbspringer/a688ab2065c2393fd479 to your computer and use it in GitHub Desktop.
Save dbspringer/a688ab2065c2393fd479 to your computer and use it in GitHub Desktop.
JS for front-end plugin
(function($) {
$(document).ready( function() {
var file_frame; // variable for the wp.media file_frame
// attach a click event (or whatever you want) to some element on your page
$( '#frontend-button' ).on( 'click', function( event ) {
event.preventDefault();
// if the file_frame has already been created, just reuse it
if ( file_frame ) {
file_frame.open();
return;
}
file_frame = wp.media.frames.file_frame = wp.media({
title: $( this ).data( 'uploader_title' ),
button: {
text: $( this ).data( 'uploader_button_text' ),
},
multiple: false // set this to true for multiple file selection
});
file_frame.on( 'select', function() {
attachment = file_frame.state().get('selection').first().toJSON();
// do something with the file here
$( '#frontend-button' ).hide();
$( '#frontend-image' ).attr('src', attachment.url);
});
file_frame.open();
});
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment