616 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Sold between 10 000 and 50 000 dollars
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
  • Sold between 10 000 and 50 000 dollars
  • Referred between 10 and 49 users
  • Bought between 10 and 49 items
  • Has been a member for 5-6 years
  • Exclusive Author
  • Envato Studio (Microlancer) Beta Tester
ChillThemes says

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

616 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Sold between 10 000 and 50 000 dollars
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.

114 posts
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Exclusive Author
  • Bought between 100 and 499 items
  • Referred between 50 and 99 users
  • Most Wanted Bounty Winner
  • Envato Studio (Microlancer) Beta Tester
  • Has been a member for 2-3 years
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'} );
    }
}
114 posts
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Exclusive Author
  • Bought between 100 and 499 items
  • Referred between 50 and 99 users
  • Most Wanted Bounty Winner
  • Envato Studio (Microlancer) Beta Tester
  • Has been a member for 2-3 years
nagaemas says

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

editor.save( { no_events: true } );
616 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Sold between 10 000 and 50 000 dollars
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!

114 posts
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Exclusive Author
  • Bought between 100 and 499 items
  • Referred between 50 and 99 users
  • Most Wanted Bounty Winner
  • Envato Studio (Microlancer) Beta Tester
  • Has been a member for 2-3 years
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 :)

616 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Sold between 10 000 and 50 000 dollars
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! :)

114 posts
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Exclusive Author
  • Bought between 100 and 499 items
  • Referred between 50 and 99 users
  • Most Wanted Bounty Winner
  • Envato Studio (Microlancer) Beta Tester
  • Has been a member for 2-3 years
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!

by
by
by
by
by
by