Insert text a cursor position in to TinyMCE text editor

To test this, I’ve added a button to one of the other meta boxes on the post edit/new screen, with an id of addtxtbtn. On click, I grabbed the textareas inside the #wp-content-editor-container, which is the default wrapper for the default WP Editor instance. You might need to alter this, if you got different MarkUp. The rest is taken from the demo on GitHub – in case this is the plugin you were talking about.

( function($) {
    $( '#addtxtbtn' ).on( 'click', function( e ) {
        e.preventDefault();
        var textarea = $( '#wp-content-editor-container' ).find( 'textarea' );
        textarea.bind( 'updateInfo keyup mousedown mousemove mouseup', function() {
            var range = $( this ).textrange();
            console.log( range );
        } );
    } );
} )( jQuery );

Text

So in short: To add text to the “Text”-textarea, do the following:

$( '#wp-content-editor-container' ).find( 'textarea' ).val( 'Some default Text' );

Visual

To add text to the “TinyMCE”https://wordpress.stackexchange.com/”Visual” editor, do the following:

tinyMCE.activeEditor.execCommand( 'mceInsertContent', false, 'Some default Text' );

If you need to get the content from TinyMCE, you can use the following:

// Raw
tinyMCE.activeEditor.getContent( { format : 'raw' } );
// HTML contents:
tinyMCE.activeEditor.getContent();

To get content from a specific TinyMCE instance (if more than one would be possible), use:

tinyMCE.get( 'some-id' ).getContent();