I’m not sure why I haven’t been able to find this, but does anyone know how to get the input of the TinyMCE editor? I am using it in the front end and want to be able to save whatever the user has typed into the TinyMCE to a database but cannot find the best way to capture that value.
The two solutions that I’ve implemented with some success are:
-
tinyMCE.activeEditor.getContent();
– This one seems to only get the value of the visual editor so if I’m in the HTML editor and make changes and then save, they aren’t picked up. -
$('#html_text_area_id').val();
– This one is the opposite, it only seems to get the value of the HTML editor.
I know there is a better way – I just can’t seem to find it…
p.s Yes, I’m going to implement security measures to make sure people can’t blow up the database.
Ok apparently WordPress keeps track of what kind of editor (visual or html) is active as a class which is added to the content wrapper so here is a solution that will get you the latest content in the editor
That’s the code I added to my javascript, right before the form is submitted.
This worked for me:
Where description is the ID of the tinymce editor and de code after the else of the accepted answer, did not work for me.
I required a lot more code to get it working, and also was getting a javascript error:
Deprecated TinyMCE API call: <target>.onKeyUp.add(..)
This was caused by a wordpress upgrade from 3.x to 4. So I had toclear my browser cache
first.Firstly I added a callback to teh wp filter
tiny_mce_before_init
in my functions.php file, this allowed to me to add a js callback function to be fired when the editors are initialised:Next the javascript function to do what ever is wanted with the content when it changes. Add this javascript using wp_enqueue_scripts to the page you want.
The code worked when I used the following to print the editor to any page:
You can get the contents depending on the current mode of the editor.
The Visual tab has a class
switch-tmce
which you can use for identifying it as a tab. You would know that it has been focused if it is having the lighter background color. So, you can also use that to determine which of the two tabs is active.This is an adaptive solution based on Bainternet‘s answer. There are probably other better ways to do it but this one worked well for me.
You can also use
addeditor
event :TinyMCE ‘addeditor’ event documentation