WP editor TinyMCE: set content ALSO for textarea under text tab

I have a thing that inserts html/code inside the wordpress post editor.
Everything works perfect.

There are multiple ways and functions to do so, BUT: they do not set the content when viewing the text tab.

I tried a billion things, i must be missing something :slight_smile:

Anyone know how to do that? So setting editor content while viewing the ‘text’ tab…

You can add your own quicktags to the Text mode. Have a look at this: http://codex.wordpress.org/Quicktags_API

ChillThemes said

You can add your own quicktags to the Text mode. Have a look at this: http://codex.wordpress.org/Quicktags_API

Yes, but it’s an external function, no button in the editor needed.

Also working on this recently for my page builder, this does work:

if( typeof tinymce != "undefined" ) {
	var editor = tinymce.get( 'content' );
	if( editor && editor instanceof tinymce.Editor ) {
		var content = 'your content';
		editor.setContent( content, {format : 'html'} );
	}
}

Just discovered about the tinymce text tab, solved it using the following code after setContent

editor.save( { no_events: true } );

@nagaemas - this works great!

it perfectly saves the html editor’s content to the textarea of the text/html tab itself.

BUT there’s still one tiny problem: Say you’re editing in text mode with the text tab active. WordPress remembers that. So the next time you edit a post or page it initialy has the text tab active - and then there’s no active editor set etc. the instance can’t be found and handled UNTIL you have clicked the Visual tab.

So i’m almost there but now i’m trying to find a solution for that last small issue.

I’ll post it when i find something!

ChapterThemes said

@nagaemas - this works great!

it perfectly saves the html editor’s content to the textarea of the text/html tab itself.

BUT there’s still one tiny problem: Say you’re editing in text mode with the text tab active. WordPress remembers that. So the next time you edit a post or page it initialy has the text tab active - and then there’s no active editor set etc. the instance can’t be found and handled UNTIL you have clicked the Visual tab.

So i’m almost there but now i’m trying to find a solution for that last small issue.

I’ll post it when i find something!

Thanks to your question I also discovered this!

If you look at wp-admin/js/editor.js and the go function you’ll see that the editor is instantiated if the user selects the Visual tab. Maybe initializing the tinymce yourself might be a solution, but that seems to be bad practice :frowning:

I am currently also looking for the solution, let me know if you found it :slight_smile:

Woop woop, found the solution :slight_smile:

if( typeof tinymce != "undefined" ) {
	var editor = tinymce.get( 'content' );
	if( editor && editor instanceof tinymce.Editor ) {
		editor.setContent( 'CONTENT' );
		editor.save( { no_events: true } );
	}
	else {
		jQuery('textarea#content').val( 'CONTENT' );
	}
}

Simple as is, the else statement just sets the textarea value when the text tab is the initial tab since tinymce is not initialized before having the visual tab active in any way.

Hope it’s usefull for you and thanks for sharing! :slight_smile:

ChapterThemes said

Woop woop, found the solution :slight_smile:

if( typeof tinymce != "undefined" ) {
	var editor = tinymce.get( 'content' );
	if( editor && editor instanceof tinymce.Editor ) {
		editor.setContent( 'CONTENT' );
		editor.save( { no_events: true } );
	}
	else {
		jQuery('textarea#content').val( 'CONTENT' );
	}
}

Simple as is, the else statement just sets the textarea value when the text tab is the initial tab since tinymce is not initialized before having the visual tab active in any way.

Hope it’s usefull for you and thanks for sharing! :slight_smile:

Great, thanks for this!

Seriously, thanks to both of you :slight_smile: