Created
June 9, 2013 19:31
-
-
Save ryanfitton/5744858 to your computer and use it in GitHub Desktop.
Read more about the usage of this script on http://ryanfitton.co.uk/blog/implementing-an-image-picker-for-wordpress-3-5/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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