I had the same problem just now, and I found a solution, so I share it here. Hope it's not too late.
First of all, to use the WP Add Media button, you will need to install the script you need. It's easy, just call the wp_enqueue_media () function as follows:
add_action('admin_enqueue_scripts', 'enqueue_scripts_styles_admin'); function enqueue_scripts_styles_admin(){ wp_enqueue_media(); }
This call ensures that you have the necessary libraries to use the WP Media button.
Of course, you should also have HTML elements to store the loaded / selected URL of the media file, something like this:
<input type="text" class="selected_image" /> <input type="button" class="upload_image_button" value="Upload Image">
In the first text box, the URL of the media file will be displayed, and the second is the button for opening the media pop-up window.
Then in your jscript you will have something like this:
var custom_uploader; $('.upload_image_button').click(function(e) { e.preventDefault(); var $upload_button = $(this);
Now I will not explain each line, because it is not so difficult to understand. The most important part is the one that uses the wp object to make it all work.
The tricky part is doing all this work on the TinyMCE popup (which I came across). I was looking for hi and lo for a solution, and here is what worked for me. But before that I will talk about what I encountered in the first place. When I first tried to implement this, I ran into the "WP is undefined" problem in a popup. To solve this problem, you just need to pass the WP object to the script as follows:
(function() { tinymce.create('tinymce.plugins.someplugin', { init : function(ed, url) { // Register commands ed.addCommand('mcebutton', function() { ed.windowManager.open( { file : url + '/editor_button.php', // file that contains HTML for our modal window width : 800 + parseInt(ed.getLang('button.delta_width', 0)), // size of our window height : 600 + parseInt(ed.getLang('button.delta_height', 0)), // size of our window inline : 1 }, { plugin_url : url, wp: wp } ); }); // Register buttons ed.addButton('someplugin_button', {title : 'Insert Seomthing', cmd : 'mcebutton', image: url + '/images/some_button.gif' }); } }); // Register plugin // first parameter is the button ID and must match ID elsewhere // second parameter must match the first parameter of the tinymce.create() function above tinymce.PluginManager.add('someplugin_button', tinymce.plugins.someplugin); })();
We are interested in this line => "wp: wp". This line ensures that we pass the wp object to the popup (iframe really ...), which should be open when we click the tinymce button. In fact, you can transfer something to the popup through this object (the second parameter of the ed.windowManager.open method)!
Last but not least, you will need to reference this passed wp object in your javascript:
var args = top.tinymce.activeEditor.windowManager.getParams(); var wp = args.wp;
Make sure you do this before calling / using the WP object.
That is all you need to do to make this work. It worked for me, hope it works for you :)