[wp-hackers] Javascript bind event for 3.5's new uploader?

Daryl Koopersmith koop at wordpress.org
Thu Dec 20 21:28:50 UTC 2012


> I'm trying to add an action when the uploader is displayed, but I
> can't seem to call the .on or .bind from external code.
>  
> Unfortunately, it seems this can't be added from external code.
> wp.media.editor.on, wp.media.editor.bind, and others will not work.


Hi Luke,

wp.media.editor is used to manage instances of editor-specific media managers. If you're looking to trigger an event when opening the default media modal, you'll want to grab a reference to the media manager by calling wp.media.editor.add('content'). We're calling "add" here instead of "get" to make sure the modal exists, because "get" may return undefined (and don't worry, "add" only creates the instance once). You can then call the .on method on that object and your code will run just fine.

For example:

var media = wp.media.editor.add('content');
media.on( 'open', function() {
console.log( 'Opening the modal…' );
});

Hope this helps.

Cheers,
Koop



More information about the wp-hackers mailing list