642 posts
  • Has been part of the Envato Community for over 3 years
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
ChapterThemes says

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 :)

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

709 posts
  • Has referred 10+ members
  • Has sold $10,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
+2 more
ChillThemes says

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

642 posts
  • Has been part of the Envato Community for over 3 years
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
ChapterThemes says

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.

123 posts
  • Has referred 50+ members
  • Has sold $125,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 100+ items on Envato Market
+5 more
nagaemas says

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'} );
    }
}
123 posts
  • Has referred 50+ members
  • Has sold $125,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 100+ items on Envato Market
+5 more
nagaemas says

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

editor.save( { no_events: true } );
642 posts
  • Has been part of the Envato Community for over 3 years
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
ChapterThemes says

@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!

123 posts
  • Has referred 50+ members
  • Has sold $125,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 100+ items on Envato Market
+5 more
nagaemas says

@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 :(

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

642 posts
  • Has been part of the Envato Community for over 3 years
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
ChapterThemes says

Woop woop, found the solution :)

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! :)

123 posts
  • Has referred 50+ members
  • Has sold $125,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 100+ items on Envato Market
+5 more
nagaemas says

Woop woop, found the solution :)
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! :)

Great, thanks for this!

1552 posts How's the surf doing?
  • Nominated Community Superstar of the month
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has been part of the Envato Community for over 3 years
  • Had an item featured on Envato Market
+7 more
tommusrhodus says

Seriously, thanks to both of you :)

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by