I’m applying tinyMCE to some textareas in WordPress metaboxes. I’m going to be adding areas dynamically so I can’t use the wp_editor()
function. I’m pretty close but I am stumped on passing some settings.
As is, the following code only seems to initialize the last textarea in the set. However, if you uncomment the var settings
declaration inside the .each()
loop, then they are all activated. I presume this has something to do with scope, because I’m totally lost as I can’t reproduce it on my fiddle.
Eventually, I would like to borrow the default post editor’s settings and then just change the selector… possibly via jQuery’s .extend()
. I suppose I could run the .extend()
every time withing the .each()
, but that seems inefficient.
jQuery(document).ready(function ($) {
var settings = { menubar : false };
$('textarea.wp-editor-area').each( function( i ) {
var id = $(this).attr( 'id' );
if ( !id) {
id = 'mceEditor-' + ( i );
$( this ).attr( 'id', id );
}
//var settings = { menubar : false };
settings.selector = "#" + id ;
try {
tinymce.init( settings );
} catch(e) {}
}); //end each
});
http://jsfiddle.net/helgatheviking/gDNdz/5/
On another note, the .each()
loop works if I use tinymce.execCommand( 'mceAddEditor', true, id );
Is there any noticeable difference between the .execCommand()
and .init()
? It also works if I use a different tinyMCE mode (such as specific_textareas), I am just now really curious why it would work in my fiddle but not in my site.
Still can’t comment…
Rather than adding them using their ID, why can’t you use:
This would init all matching textareas? That way there’s no need for a loop.