我如何在前端包含Tinymce编辑器?
题
我正在尝试在用户可以发布的地方添加一位Tinymce编辑器,但到目前为止没有运气。这是代码:
PHP:
add_action('wp_print_scripts', 'my_enqueue_scripts');
function my_enqueue_scripts() {
wp_enqueue_script( 'tiny_mce' );
if (function_exists('wp_tiny_mce')) wp_tiny_mce();
}
JavaScript:
jQuery(document).ready(function(){
tinyMCE.init({
mode : "textareas",
theme : "simple",
/*plugins : "autolink, lists, spellchecker, style, layer, table, advhr, advimage, advlink, emotions, iespell, inlinepopups, insertdatetime, preview, media, searchreplace, print, contextmenu, paste, directionality, fullscreen, noneditable, visualchars, nonbreaking, xhtmlxtras, template",*/
editor_selector :"editor"
});
});
html:
<textarea rows="8" cols="40" name="description" id="editor" class="required"><?php echo $description;?></textarea>
问题: Texteditor不添加到Textarea。尽管Tinymce JS文件正在加载。
解决方案
好吧,感谢WP 3.3现在我们有 wp_editor()
做到这一点的功能:)
其他提示
editor_selector
用于定位类,而不是ID。
另外,使用 editor_selector
, ,必须设置 mode: "specific_textareas"
为了使其工作。
看 http://tinymce.moxiecode.com/wiki.php/configuration:editor_selector
因此,您的JavaScript和HTML应该看起来像这样:
jQuery(document).ready(function(){
tinyMCE.init({
mode : "specific_textareas",
theme : "simple",
/*plugins : "autolink, lists, spellchecker, style, layer, table, advhr, advimage, advlink, emotions, iespell, inlinepopups, insertdatetime, preview, media, searchreplace, print, contextmenu, paste, directionality, fullscreen, noneditable, visualchars, nonbreaking, xhtmlxtras, template",*/
editor_selector :"tinymce-enabled"
});
});
<textarea rows="8" cols="40" name="description" id="editor" class="tinymce-enabled required"><?php echo $description;?></textarea>
Altough @maryisdead的答案可能是对的,我会给您另一个提示,首先确保页面中只有一个元素,其中ID =“编辑器”,然后设置tinymce。
tinyMCE.init({
...
mode : "exact",
elements : "editor"
});
另外,请使用jQuery代替JavaScript代码中的$来确保您正在调用jQuery方法和选择器。
editor_selector用于类,而不是ID。
您应该将editor_selector的值用作TextArea的类名。