Skip to content

Instantly share code, notes, and snippets.

@ryanfitton
Created June 9, 2013 19:32
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/5744867 to your computer and use it in GitHub Desktop.
Save ryanfitton/5744867 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>
<div style='clear:both;margin-bottom:10px;overflow:auto;'>
<label>Image 1: </label>
<input name='image_1_port' id='image_1_port' style='width:200px' />
<input id='_btn' class='upload_image_button' type='button' value='Choose Image 1' />
</div>
<div style='clear:both;margin-bottom:10px;overflow:auto;'>
<label>Image 2: </label>
<input name='image_2_port' id='image_2_port' style='width:200px' />
<input id='_btn' class='upload_image_button' type='button' value='Choose Image 2' />
</div>
<div style='clear:both;margin-bottom:10px;overflow:auto;'>
<label>Image 3: </label>
<input name='image_3_port' id='image_3_port' style='width:200px' />
<input id='_btn' class='upload_image_button' type='button' value='Choose Image 3' />
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment