Last active
April 3, 2017 07:58
-
-
Save bitfade/4460948 to your computer and use it in GitHub Desktop.
WordPress 3.5 media upload api example
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
var workflow = wp.media({ | |
title: 'Select the images', | |
// use multiple: false to disable multiple selection | |
multiple: 'add', | |
button: { | |
text: 'Add selected images' | |
}, | |
library: { | |
type: 'image' | |
} | |
}); | |
function getAttachment(attachment) { | |
attachment = attachment.toJSON(); | |
return {id:attachment.id,url:attachment.url}; | |
} | |
function select() { | |
// use this to unbind the event | |
// workflow.off("select"); | |
// get all selected images (id/url attributes only) | |
console.log(workflow.state().get('selection').map(getAttachment)); | |
} | |
function reset() { | |
// called when dialog is closed by "close" button / "ESC" key | |
// use the following line unbind the event | |
// workflow.off("select"); | |
} | |
// bind event handlers | |
workflow.on("select",select); | |
workflow.on("escape",reset); | |
// open the dialog | |
workflow.open(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment