Skip to content

Instantly share code, notes, and snippets.

Created December 30, 2012 01:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4410366 to your computer and use it in GitHub Desktop.
Save anonymous/4410366 to your computer and use it in GitHub Desktop.
How to create a reference to a WordPress 3.5 media manager
Date: Thu, 20 Dec 2012 16:28:50 -0500
From: Daryl Koopersmith <koop@wordpress.org>
Subject: Re: [wp-hackers] Javascript bind event for 3.5's new
uploader?
To: wp-hackers@lists.automattic.com
Message-ID: <DB193711A3AC46F1AA76AAE5940586BA@gmail.com>
Content-Type: text/plain; charset="utf-8"
> 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment