Skip to content

Instantly share code, notes, and snippets.

@Zenger
Created February 13, 2014 09:19
Show Gist options
  • Save Zenger/8972156 to your computer and use it in GitHub Desktop.
Save Zenger/8972156 to your computer and use it in GitHub Desktop.
// IMPORTANT ! Use "wp_enqueue_media()" to enqueue the media manager
/*
Usage :
Media( jQuery('my-element'), { title: "Select" } , function(img) {
// do something with the img (note for multiple is going to be an array!)
});
*/
function Media(element, settings, cb)
{
this.settings = jQuery.extend({
element : element, // bind to element
multiple : false, // handle multiple uploads
onInput : false, // automatically insert value
title : "Upload image", // text
buttonText : "Insert", // text
}, settings);
this.element = element;
this.frame = {};
_this = this;
this.settings.element.on('click', function() {
_this.frame.open();
});
this.frame = wp.media.frames.frame = wp.media({
title : this.settings.title,
button: {
text : this.settings.buttonText
},
multiple: this.settings.multiple
});
this.frame.on('select', function() {
if (!_this.settings.multiple)
{
attachment = _this.frame.state().get('selection').first().toJSON();
}
else
{
attachment = _this.frame.state().get('selection').toJSON();
}
if (typeof cb == "function") {
cb(attachment);
}
if (_this.settings.onInput && !_this.settings.multiple)
{
_this.settings.onInput.val( attachment.url );
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment