Array

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

C# How to pass ulong argument to unit test via TestCase decorator

I have a unit test where I want to pass some arguments into the test method via the TestCase decorator. The argument I need...

Can google home turn on my smart tv?

If I purchase a Google hub and set up everything I need to use Google Home to turn my house into a smart house....

Can amazon smart plug work with google home?

I am looking to purchase some smart plugs and I see that there is an amazon branded smart plug. It is advertised to work...

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

Bitrate Converter

Below you will find a bitrate converter. This tool will allow you to enter a bitrate value, in one of many different formats and...

Aesthetic Text Generator

There are various ways to make your social media profile seem more unique, some of which are not as easy to implement as others....

Aspect Ratio Calculator For Images

Aspect ratio is the ratio between the height and width of an image. If you want to resize an image by 100 pixels, you...

Add Text To Image

Use this free tool to add text to an image. Simply select the image file that you want to overlay text onto and you...

JavaScript Multi-line String Builder

Javascript did not always support multi-line strings. If you attempted to create a string variable using quotes, putting a line break into the source...

GUID Generator

GUID generation is relatively simple. It is a system that is used to generate unique values without having to keep track of the IDs...

Latest Posts

Latest Questions