Skip to content

Instantly share code, notes, and snippets.

@zogot
Last active December 23, 2015 23:39
Show Gist options
  • Save zogot/6711359 to your computer and use it in GitHub Desktop.
Save zogot/6711359 to your computer and use it in GitHub Desktop.
Pronamic WordPress Media jQuery Plugin
pronamicMedia = function(elem, options) {
this.elem = elem;
this.$elem = jQuery(elem);
this.options = options;
};
pronamicMedia.prototype = {
_frame:undefined
, defaults: {}
, setDefaults: function() {
this.defaults.$target = jQuery(this.$elem.data('target'));
this.defaults.mediaOptions = {
title:this.$elem.data('title')
, library: { type: 'image' }
, button: { text: this.$elem.data('update-text')}
, multiple: false
};
}
, init: function() {
this.setDefaults();
this.config = jQuery.extend({}, this.defaults, this.options);
this.binds();
return this;
}
, binds: function() {
var self = this;
this.$elem.on('click', function(e) {
e.preventDefault();
self.frame().open();
} );
}
, frame: function() {
var self = this;
if(this._frame)
return this._frame;
this._frame = wp.media(this.config.mediaOptions);
this._frame.state('library').on('select', function() {
var selection = this.get('selection');
self.config.$target.val( selection.pluck('id') );
});
return this._frame;
}
};
jQuery.fn.pronamicMedia = function(options) {
return this.each( function() {
new pronamicMedia(this, options).init();
} );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment