Skip to content

Instantly share code, notes, and snippets.

@marcusig
Last active January 26, 2018 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcusig/8caf1372fc8d929948035185cb2d5aaf to your computer and use it in GitHub Desktop.
Save marcusig/8caf1372fc8d929948035185cb2d5aaf to your computer and use it in GitHub Desktop.
wp.media listen to changes / sync when opening a media modal
(function($,wp){
var handle_attachment_change = function(attachment){
console.log(attachment);
}
var media_modal = wp.media({
multiple: 'add',
library: {type: 'image'},
button: {
text: 'Select image'
}
});
media_modal.on('attach', function(){
var library = update_slide_frame.state().get('library');
// listen to the change event on the library items
media_modal.listenTo(library, 'change', function(attachment)){
// do something with the changed model
handle_attachment_change(attachment);
};
// listen to the Sync event on the library
media_modal.listenTo(library, 'sync', function(attachment)){
// do something with the changed model
handle_attachment_change(attachment);
};
});
})(jquery, wp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment