Skip to content

Instantly share code, notes, and snippets.

@romainberger
Created February 24, 2014 22:50
Show Gist options
  • Save romainberger/9198978 to your computer and use it in GitHub Desktop.
Save romainberger/9198978 to your computer and use it in GitHub Desktop.
Wordpress admin lightbox function
/**
* Simple wrapper to use the media lightbox in custom scripts
*
* Usage:
*
* openMediaLightbox(function(attachment) {
* // do something with attachment
* })
*/
!function($) {
'use strict';
var file_frame
var openMediaLightbox = function(cb) {
// If the media frame already exists, reopen it
if (file_frame) {
file_frame.open()
return
}
// Create the media frame
file_frame = wp.media.frames.file_frame = wp.media({
title: $(this).data('uploader-title')
, button: {
text: $(this).data('uploader-button-text')
}
, multiple: false
})
file_frame.open()
// callback once the file is selected
file_frame.on('select', function() {
var attachment = file_frame.state().get('selection').first().toJSON()
cb(attachment)
})
}
// export
window.openMediaLightbox = openMediaLightbox
}(window.jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment