WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] New Media Manager - close/unload event (7 posts)

  1. Martin Snajdr
    Member
    Posted 1 year ago #

    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.

  2. Martin Snajdr
    Member
    Posted 1 year ago #

    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
       });
    }
  3. Nikola Nikolov
    Member
    Posted 1 year ago #

    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).

  4. Martin Snajdr
    Member
    Posted 1 year ago #

    Thank you for your reply, much improved snippet!

  5. revaxarts
    Member
    Posted 1 year ago #

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

    I guess this is more future proof

  6. programmin
    Member
    Posted 1 year ago #

    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

  7. programmin
    Member
    Posted 12 months ago #

    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! :)

Topic Closed

This topic has been closed to new replies.

About this Topic