631 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…

707 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

631 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.

117 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'} );
    }
}
117 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 } );
631 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!

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

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

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

1397 posts How's the surf doing?
  • Has referred 200+ members
  • Has sold $500,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
+6 more
tommusrhodus says

Seriously, thanks to both of you :)

by
by
by
by
by
by