TinyMCE in WordPress Plugins

Jan 04, 2010
11

Unlike some of the other JavaScript assets available to plugin developers through wp_enqueue_script(), getting TinyMCE to function in your plugin is a little different. You would perhaps expect to use it like this:

<?php
add_action("admin_print_scripts", "js_libs");

function js_libs() {
     wp_enqueue_script('tiny_mce');
}
?>
<script type="text/javascript">

	tinyMCE.init({
		mode : "exact",
		elements: "a_nice_textarea",
		theme : "simple"
	});

</script>
<textarea id="a_nice_textarea" name="a_nice_textarea"></textarea>

However, enqueuing the TinyMCE really won’t get you any results. You need to use the wp_tiny_mce() function, which is not well documented. Essentially, the function pre-constructs the TinyMCE for you, and you override its default options. The main option to override is editor_selector which points TinyMCE at a specific textarea class.

<?php
wp_tiny_mce( false , // true makes the editor "teeny"
	array(
		"editor_selector" => "a_nice_textarea"
	)
);
?>

<textarea class="a_nice_textarea" id="a_nice_textarea" name="a_nice_textarea"></textarea>

You can stick this anywhere you want, and it will work (for the most part). All the available configuration options for TinyMCE can be determined by referencing it within the array. WordPress has a bunch of them determined by default for the post content editor; most of them are fine as is.

// TinyMCE init settings
$initArray = array (
	'mode' => 'none',
	'onpageload' => 'switchEditors.edInit',
	'width' => '100%',
	'theme' => 'advanced',
	'skin' => 'wp_theme',
	'theme_advanced_buttons1' => "$mce_buttons",
	'theme_advanced_buttons2' => "$mce_buttons_2",
	'theme_advanced_buttons3' => "$mce_buttons_3",
	'theme_advanced_buttons4' => "$mce_buttons_4",
	'language' => "$mce_locale",
	'spellchecker_languages' => "$mce_spellchecker_languages",
	'theme_advanced_toolbar_location' => 'top',
	'theme_advanced_toolbar_align' => 'left',
	'theme_advanced_statusbar_location' => 'bottom',
	'theme_advanced_resizing' => true,
	'theme_advanced_resize_horizontal' => false,
	'dialog_type' => 'modal',
	'relative_urls' => false,
	'remove_script_host' => false,
	'convert_urls' => false,
	'apply_source_formatting' => false,
	'remove_linebreaks' => true,
	'paste_convert_middot_lists' => true,
	'paste_remove_spans' => true,
	'paste_remove_styles' => true,
	'gecko_spellcheck' => true,
	'entities' => '38,amp,60,lt,62,gt',
	'accessibility_focus' => true,
	'tab_focus' => ':prev,:next',
	'content_css' => "$mce_css",
	'save_callback' => 'switchEditors.saveCallback',
	'wpeditimage_disable_captions' => $no_captions,
	'plugins' => "$plugins"
);

For a closer look at wp_tiny_mce(), check out the source.

  • Thanks a lot, very appreciated.
  • Darren
    Is it possible to get te Visual/HTML tabs to display using this method?
    It also doesn't seem to be formatting the text properly for me, it's ignoring carriage returns.
  • Do you have any advice for including the editor on non admin pages?

    In particular what files would need to be included.

    Any help would be much appreciated.
  • keighl
    You can probably use this method for front-end editors. I've never done that, but the same libraries are available, so I don't see why it wouldn't work.
  • Sure. Just doesn't seem to be so straightforward, I think several other files need to be included. I shall work it out.

    Btw, your link to wp_tiny_mce() in the source points to the wrong file. It should be http://core.trac.wordpress.org/browser/trunk/wp...
  • Stevish
    This doesn't work at all for me in the admin area :(

    All the text is white (on a white background) and it's stuck in the HTML mode. I click on "Visual" but nothing happens. Is it because I'm trying it in the admin area?
  • keighl
    What exactly are trying to do?
  • Thanks for the tip! This worked like a charm.
  • keighl
    Please note the update here ... 'editor_selector' points to a class not an id.
  • The only other thing I had to do was give the textarea a class called "theEditor"

    Thank you very much for this blog post, it helped tremendously!
  • keighl
    thanks, gregg! glad it helped.
blog comments powered by Disqus
Keighl

© 2010 Keighl

1.508.265.3032
info@keighl.com