With this simple and relative short code you can add the WordPress 3.5 Media Uploader to your own plugins. It will return single or multiple images and returns them in a comma seperated list.
If you want to return other attributes then monitor the attributes using the console.
jQuery(document).ready(function(jQuery){ jQuery('#image_button').click(function(e) { e.preventDefault(); frame = wp.media({ title : 'Add your title here', frame: 'post', multiple : true, // set to false if you want only one image library : { type : 'image'}, button : { text : 'Add Image' }, }); frame.on('close',function(data) { var imageArray = []; images = frame.state().get('selection'); images.each(function(image) { imageArray.push(image.attributes.url); // want other attributes? Check the available ones with console.log(image.attributes); }); jQuery("#imageurls").val(imageArray.join(",")); // Adds all image URL's comma seperated to a text input }); frame.open() }); });
<input type="text" name="imageurls" id="imageurls" /> <input type= "button" class="button" name="image_button" id="image_button" value="Add Image(s)" />
Hey man!
You saved us a lot of time, thank you very much!
Cheers from Switzerland
Chris
Great, exactly whatr I was looking after. Works like a charm. I now try to make a preview of the selected image in my metabox