Skip to content

Instantly share code, notes, and snippets.

@halfempty
Created February 12, 2013 18:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save halfempty/4771812 to your computer and use it in GitHub Desktop.
Save halfempty/4771812 to your computer and use it in GitHub Desktop.
var file_frame;
// "mca_tray_button" is the ID of my button that opens the Media window
jQuery('#mca_tray_button').live('click', function( event ){
event.preventDefault();
if ( file_frame ) {
file_frame.open();
return;
}
file_frame = wp.media.frames.file_frame = wp.media({
title: jQuery( this ).data( 'uploader_title' ),
button: {
text: jQuery( this ).data( 'uploader_button_text' ),
},
multiple: false
});
file_frame.on( 'select', function() {
attachment = file_frame.state().get('selection').first().toJSON();
// "mca_features_tray" is the ID of my text field that will receive the image
// I'm getting the ID rather than the URL:
jQuery("#mca_features_tray").val(attachment.id);
// but you could get the URL instead by doing something like this:
jQuery("#mca_features_tray").val(attachment.sizes.thumbnail.url);
// and you can change "thumbnail" to get other image sizes
});
file_frame.open();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment