Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<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
You can’t perform that action at this time.