Created
February 12, 2013 18:00
-
-
Save halfempty/4771812 to your computer and use it in GitHub Desktop.
A demo for Gydo. Based on this: http://mikejolley.com/2012/12/using-the-new-wordpress-3-5-media-uploader-in-plugins/
This file contains hidden or 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
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