Switching Visual/HTML Modes With TinyMCE
One of the greatest parts of the WordPress interface is the flexibility it gives us from within the editing screen. As a user, I can write this post using the rich-editor (TinyMCE), or switch to HTML mode to edit the markup directly. I've received a handful of comments on my previous post regarding TinyMCE in WordPress Plugins asking how to go about incorporating this flexibility into plugins.
TinyMCE has a few built-in methods that allow you to turn the editor on and off; essentially this is all you have to do to switch between the two modes. It is not, however, a second interface. The WordPress editor actually has two rich-editors: one for Visual, and one for HTML. Instead of turning off TinyMCE, WordPress merely switches the rich-editors. That's why you see those nice formatting buttons even through, you're in HTML mode. Accomplishing that type of functionality is difficult. You can see the complexity of the custom switchEditors method in editor.dev.js.
Too much to cover. For now, I will simply show you how to turn the rich-editor on and off. First, set up a basic TinyMCE editor. This code builds of my other post. There you can find a more complete description of what's going on. In short, though, wptinymce() does all the initialization for you, and queues it on the page. This example could exist on a plugin options page, the profile editor, a post metabox, or really anywhere you want users to rich-edit.
wp_tiny_mce(false, // true makes the editor "teeny"
array(
"editor_selector" => "foo",
"height" => 150
)
);
<textarea class="foo" id="foo" name="foo"></textarea>
Although you must setup the editor for your plugin using wptinymce(), the regular TinyMCE methods are all available to call from some custom JavaScript. The ones you want to be concerned with are mceAddControl and mceRemoveControl. If you spent any time digging through editor.dev.js, you would notice that the complex WordPress switcher still uses these methods.
Above the textarea, stick some links that will act as our switchboard:
<p align="right">
<a class="button toggleVisual">Visual</a>
<a class="button toggleHTML">HTML</a>
</p>
<textarea class="foo" id="foo" name="foo"></textarea>
Now, write some simple jQuery that adds the switching functionality. You could enqueue this as a separate file, or place it with the textarea.
var id = 'foo';
$('a.toggleVisual').click(
function() {
tinyMCE.execCommand(‘mceAddControl’, false, id);
}
);
$('a.toggleHTML').click(
function() {
tinyMCE.execCommand(‘mceRemoveControl’, false, id);
}
);
The buttons now add or remove the rich-editor respectively. You will notice that, when in HTML mode, the textarea resolves to a very small box. To combat this problem, simply give it some rows and cols markup, or style it with CSS.
Another route in you could take is a single button that toggles the editor on or off. I use this in my TinyMCE Signature plugin. It's not terribly different; just add some logic.
$('a.toggleEditor').click(
function() {
var id = 'foo';
if (tinyMCE.get(id)) {
tinyMCE.execCommand('mceRemoveControl', false, id); }
else {
tinyMCE.execCommand('mceAddControl', false, id);
}
}
);