Created
March 12, 2018 13:46
-
-
Save igmoweb/61c08c89bad4bf3ba013f22972915598 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const postId = 1000; | |
const frames = {}; | |
/** | |
* Get a Media Modal instance for a given item ID | |
* | |
* @param itemId | |
* @return {object} | |
*/ | |
function getMediaFrame( itemId ) { | |
if ( frames[ itemId ] ) { | |
return frames[ itemId ]; | |
} | |
const options = { | |
title: 'Select teaser image', | |
button: { text: 'Add the teaser image' }, | |
multiple: false, | |
frame: 'post' | |
}; | |
window.wp.media.view.settings.post = { | |
id: itemId | |
}; | |
frames[ itemId ] = window.wp.media( options ); | |
return frames[ itemId ]; | |
} | |
/** | |
* Open an instance of Media Modal and execute a callback when a picture is selected | |
* | |
* @param itemId | |
* @param callback | |
*/ | |
export function openMediaFrame( itemId, callback ) { | |
const frame = getMediaFrame( itemId ); | |
frame.on( 'insert', () => { | |
callback.apply( null, [ frame ] ); | |
frame.reset(); | |
} ); | |
frame.open(); | |
} | |
openMediaFrame( postId, ( frame ) => { | |
// Do something | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment