我需要更多的代码才能让它工作,而且还遇到了一个javascript错误:Deprecated TinyMCE API call: <target>.onKeyUp.add(..)
这是由wordpress从3升级引起的。x到4。所以我不得不clear my browser cache
第一
首先,我向wp过滤器添加了一个回调tiny_mce_before_init
在我的功能中。php文件,这允许我添加一个js回调函数,以便在初始化编辑器时启动:
add_filter( \'tiny_mce_before_init\', array( $obj, \'filter_cb_mce_before_init\' ) );
/**
* Filter cb to add event listeners to tinymce editor.
* @param array $init An array of setup js functions as strings.
* @return array Returns new array of js function strings.
*/
function filter_cb_mce_before_init( array $init ) {
$init[\'setup\'] = "function(ed){
if(get_tinymce_content) //not required, I use it as flag to check if on correct page
ed.on(\'change\', function(){ get_tinymce_content() });
}";
return $init;
}
接下来,使用javascript函数在内容更改时对其执行所需的操作。使用添加此javascript
wp_enqueue_scripts 转到所需页面。
/**
* Get the content of the tinyMCE editor.
* @link http://wordpress.stackexchange.com/questions/42652/how-to-get-the-input-of-a-tinymce-editor-when-using-on-the-front-end
* @return {string} Returns the content
*/
function get_tinymce_content(){
//change to name of editor set in wp_editor()
var editorID = \'my_editor_id\';
if (jQuery(\'#wp-\'+editorID+\'-wrap\').hasClass("tmce-active"))
var content = tinyMCE.get(editorID).getContent({format : \'raw\'});
else
var content = jQuery(\'#\'+editorID).val();
console.log(content);
}
当我使用以下命令将编辑器打印到任何页面时,代码都起作用:
<?php wp_editor( @$event->description, \'my_editor_id\', array(
\'media_buttons\' => false,
\'textarea_name\' => \'data[description]\',
\'quicktags\' => array("buttons"=>"link,img,close"),
) ); ?>