Use WordPress Media Manager With TinyMCE

This tutorial will show you how to take the media manager from WordPress and use it on an external page with a stock version of TinyMCE. I saw some posts about this and people were calling the WordPress editor to achieve this. Its an option, but it is not what this guide is for. If you have downloaded a standard version of TinyMCE and want the be able to browse and upload images using the WordPress media manager then this guide will show you how to do it. There are several file managers for TinyMCE, all which work perfectly fine. I have always found the WordPress media manager to be very powerful and easy to use. Unfortunately its part of WordPress and cant be downloaded on its own. Thankfully I have been able to find a way to use the media uploader within TinyMCE a lot more easy than i though. Here is how to use WordPress media manager with TinyMCE.

The first thing you will need to do is be able to load up the WordPress media manager on the page you are using. This means you need to either load the core of WordPress or just call the media method from WP. The following code will load the WordPress core and que all of the files you need to be able to call the media manager.

require_once( 'wp-load.php' );
wp_enqueue_media();

The next thing to do is create a javascript function that will load up the WordPress media manager. The following function will do this and will then return the selected image to the instance of TinyMCE that triggered it. The reason for this, is you may have more than one instance of TinyMCE on the page and you want to make sure you return data to the correct one.

<script type="text/javascript">
function wpmediabrowser(field_name, url, type, win)
{
	var image = wp.media({ 
		title: 'Upload Image',
		multiple: false
	}).open().on('select', function(e){
	var uploaded_image = image.state().get('selection').first();
		jQuery("#" + field_name).val(uploaded_image["attributes"]["url"]);
		var arr = field_name.split('-');
		var field = arr[0];
		var number = parseInt(field.split('_')[1]) + 1;
		var descriptionid = field.split('_')[0] + "_" + number;
		jQuery("#" + descriptionid).val(uploaded_image["attributes"]["description"]);
	});
}
</script>

You can test if the function works by calling it using a href. The final step to do is to link this function in with the init of TinyMCE. The following code is all you need

"file_browser_callback: wpmediabrowser,";

This variable in the TinyMCE init will tell the app that you are using a file manager and to call a function called “wpmediabrowser” when the browse button is clicked. When you select the image from the WordPress file manager it will return the image url back into the box.

Related Articles

Related Questions

Did I accidentally download malware from a suspicious link?

I clicked on a link for a language learning app that redirected me to a really flashy website, kind of like a matrix pattern...

Help with USB Eject Sound Causing Frame Drops and Black Screens

I've been dealing with this annoying issue for a bit now. I initially thought the problem was caused by a USB device ejecting. However,...

Computer Won’t Boot After Sleeping – Need Help!

Hey everyone! I came home after being out for a couple of hours, and when I tried to wake my computer from sleep, it...

1 COMMENT

  1. Hello Dan, thanks for posting this, but I’m unsure how the wp javascripts get loaded. You say just call wp_enqueue_media() but that just queues them in php, but doesn’t send them to the browser, so wp.media() is not available in wpmediabrowser(). There seems to be something missing after wp_enqueue_media(). Please could you check your source code and advise what’s missing? Much appreciated.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Latest Tools

OpenAI Token Calculator

This tool is a simple OpenAI token calculator, web-based utility designed to help you quickly estimate the number of tokens in your text when...

List Sorting Tool

Welcome to our innovative list ordering and management tool. This next-level platform enables you to sort a list of items in ascending or descending...

Sudoku Solver

Welcome to our free online Sudoku solving tool, an interactive platform for puzzle enthusiasts seeking a break from a Sudoku conundrum. This advanced platform...

Apply Image Filters To Image

Digital imagery in the modern world is all about reinforcing emotions and stories behind each photo we take. To amplify this storytelling, we are...

Add Watermark To Image

As the world is increasingly consumed by digital media, protecting your original images is paramount. We are thrilled to introduce you to our innovative...

CSV To Xml Converter

Welcome to our CSV to XML converter tool, a convenient and user-friendly solution for all your data conversion needs. This versatile tool on our...

Latest Posts

Latest Questions