Inside a WordPress theme I am developing, i’ve a TinyMCEPopup to add shortcode to the editor, some shortcode requires images. Can i add an “Add media” button which opens the WordPress media uploader and allow the user to select or upload an image even if i’m inside a TinyMCEPopup?
Leave a Reply
You must be logged in to post a comment.
Don’t know if it will help, but I had the same issue and solved it like this.
In
functions.php
addThis will initialize the button you need. Now in the
tinymce_buttons.js
you’ll add something likeFirst you add the button functionality. The list of options to put in popup is given here. It’s not 100% complete, but better than the official documentation which is crap.
The first part is the button initialization. This gives you a ‘My button for media upload’ button, and on click you should get a modal with input field and a button.
On button click the media upload will open and you can select your image. On select the url will be in the input field, and you’ll get it in your shortcode.
Hope it helps 🙂
There was another question like this (Open/Access WP Media library from tinymce plugin popup window) , so I’m pasting my answer here since this is similar:
Hi – I had the same problem just now and found the solution so I’m sharing it here. I hope it’s not too late.
First to be able to use WP Add Media button you would have to enqueue the needed script. This is easy, just call the wp_enqueue_media() function like so:
This call ensures you have the needed libraries to use the WP Media button.
Of course you should also have the HTML elements to hold the uploaded/selected media file URL, something like this:
The first text field will hold the URL of the media file while the second is a button to open the media popup window itself.
Then in your jscript, you’d have something like this:
Now I’m not going to explain every line because it’s not that hard to understand. The most important part is the one that uses the wp object to make all these to work.
The tricky part is making all these work on a TinyMCE popup(which is the problem I faced). I’ve searched hi and lo for the solution and here’s what worked for me. But before that, I’ll talk about what problem I encountered first. When I first tried to implement this, I encountered the “WP is undefined” problem on the popup itself. To solve this, you just have to pass the WP object to the script like so:
What we’re interested in is this line => “wp: wp” . This line ensures that we are passing the wp object to the popup window (an iframe really…) that is to be opened when we click the tinymce button. You can actually pass anything to the popup window via this object (the 2nd parameter of the ed.windowManager.open method)!
Last but not the least you’d have to reference that passed wp object on your javascript like so:
Make sure you do that before calling/using the WP object.
That’s all you have to do to make this work. It worked for me, I hope it works for you 🙂