我需要更多代码才能使其正常运行,并且还遇到了JavaScript错误:Deprecated TinyMCE API call: <target>.onKeyUp.add(..)
这是由将wordpress从3.x升级到4引起的clear my browser cache
。因此,我必须首先这样做。
首先,我tiny_mce_before_init
在functions.php文件中向wp过滤器添加了一个回调,这使我可以添加一个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函数可以在内容更改时执行所需的操作。使用wp_enqueue_scripts将此JavaScript添加到所需页面。
/**
* 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"),
) ); ?>
val()