Skip to content

Instantly share code, notes, and snippets.

@ryanfitton
Created June 9, 2013 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanfitton/5744858 to your computer and use it in GitHub Desktop.
Save ryanfitton/5744858 to your computer and use it in GitHub Desktop.
<script type='text/javascript'>
var file_frame;
jQuery('.upload_image_button').live('click', function( event ){ // '.upload_image_button' is the ID of my button that opens the Media window
formfield = jQuery(this).prev().attr('name'); // Needed for the script to send the URL to the appropriate text box
event.preventDefault();
if ( file_frame ) {
file_frame.open();
return;
}
file_frame = wp.media.frames.file_frame = wp.media({
title: 'Choose a Portfolio Image',
button: {
text: 'Choose Image'
},
multiple: false // Set to true to allow multiple files to be selected
});
file_frame.on( 'select', function() {
attachment = file_frame.state().get('selection').first().toJSON();
jQuery('#'+formfield).val(attachment.url);
});
file_frame.open();
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment