Support » Plugins » New Media Manager – close/unload event

  • Resolved Martin Šnajdr

    (@martin-snajdr)


    Hello,

    my plugin is using tb_unload event to create a custom callback when Upload Media thickbox window is closed. As we know, WordPress 3.5 is no longer using thickbox. So my question is, what event does the new media manager trigger when is closed? I was digging into source code, but I didn’t found solution yet.

    Thanks.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Ok, I found the solution (maybe not the best, but it’s working).

    if(typeof wp !== 'undefined'){
       var frame = wp.media.editor.add('content');
       frame.on('escape', function(){
          //Do some stuff here
       });
    }

    Hey Martin,

    First a big thanks for the code snippet! It does work – I’ve been banging my head on how I can hook to those events for a couple of hours over the last two days. I have just a little correct(although it’s for really unusual cases, so it should work most of the time):

    if(typeof wp !== 'undefined'){
    	var ed_id = $('.wp-media-buttons:eq(0) .add_media').attr( 'data-editor' ); // We get the "data-editor" attribute from the "Add Media button first
    	var ed_media = wp.media.editor.get( ed_id ); // Then we try to first get the editor
    	ed_media = 'undefined' != typeof( ed_media ) ? ed_media : wp.media.editor.add( ed_id ); // If it hasn't been created yet, we create it
    	if ( ed_media ) {
    		ed_media.on('escape', function(){ // You can also use "open" here for when the editor opens
    			_custom_media = false;
    		});
    	}
    }

    Basically this will make sure that we always use the proper editor ID(it should be “content” almost always the time, but can change sometimes).

    Thank you for your reply, much improved snippet!

    You can also get the id with id():
    var ed_id = wp.media.editor.id();

    I guess this is more future proof

    I was using wp.media.editor.add, but be warned that it is changing in 3.6: instead of returning the existing uploader, it will always create a new one. So if you call add before wordpress core, there will be two instances created, and the upload-file functionality breaks:

    https://core.trac.wordpress.org/ticket/24062

    You can disregard my previous comment – at the time the scheduled release date was just a few days away, but it was moved back, and it seems this has been fixed! 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘New Media Manager – close/unload event’ is closed to new replies.