Created
February 24, 2014 22:50
-
-
Save romainberger/9198978 to your computer and use it in GitHub Desktop.
Wordpress admin lightbox function
This file contains 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
/** | |
* 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