[英]tinyMCE.editors[] wordpress 4.8 undefined
I have a custom posttype with a metabox that can add posts with editors to the post. 我有一个带有metabox的自定义posttype,可以将带有编辑器的帖子添加到帖子中。 whenever I add a new post with an editor I want to save the content of that editor. 每当我使用编辑器添加新帖子时,我都想保存该编辑器的内容。 It has worked fine previously but has now stopped working. 以前运行正常,但现在停止工作。 I can see that there are some changes in the wordpress update 4.8 to the editor api, but I can't see how the changes affect my code. 我可以看到wordpress update 4.8中对编辑器api进行了一些更改,但是看不到更改如何影响我的代码。
Making the editor: 制作编辑器:
<?php
public function wldk_elearn_add_elements_to_metabox($subpage_id){
$parent_id = $subpage_id;
echo '<div id="element_data_input_text">';
<?php
$settings = array( 'textarea_name' => 'mycustomeditor_'.$parent_id );
$editor_id = 'mycustomeditor_'.$parent_id;
wp_editor( "", $editor_id, $settings );
echo '</div>';
}
Javascript Java脚本
function handleAddElementAction() {
$('.wldk-elearn-add-element').click(function (event) {
event.preventDefault();
var $wrapper = $(this).parents('#wldk-elearn-new-element');
var $subpage = $wrapper.find('input[name=subpage_id]');
var $type = $wrapper.find('input:radio[name=element_type]:checked');
var subpage = $subpage.val();
var content = '';
var whichmceditor = 'mycustomeditor_'+subpage;
console.log(whichmceditor);
if($type.val()=='tx'){
content = tinyMCE.editors[whichmceditor].getContent();
}
});
}
Uncaught TypeError: Cannot read property 'getContent' of undefined 未捕获的TypeError:无法读取未定义的属性'getContent'
i have also tried 我也尝试过
content = tinyMCE.get(whichmceditor).getContent();
Which just gives me 这只是给我
Uncaught TypeError: Cannot read property 'getContent' of null 未捕获的TypeError:无法读取null的属性“ getContent”
Its like tinyMCE methods don't exist anymore or something. 就像tinyMCE方法一样,它不再存在。 I am not very good at this so any help or clue would be apreciated greatly. 我不是很擅长此事,因此将不胜感激任何帮助或线索。
So i figured it out! 所以我想通了! It was simply that the editor must be in visual mode in order to get the editor. 只是为了获得编辑器,编辑器必须处于可视模式。 A simple conditional fixes the issue: 一个简单的条件可解决此问题:
function handleAddElementAction() {
$('.wldk-elearn-add-element').click(function (event) {
event.preventDefault();
var $wrapper = $(this).parents('#wldk-elearn-new-element');
var $subpage = $wrapper.find('input[name=subpage_id]');
var $type = $wrapper.find('input:radio[name=element_type]:checked');
var subpage = $subpage.val();
var content = '';
var whichmceditor = 'mycustomeditor_'+subpage;
console.log(whichmceditor);
if($type.val()=='tx'){
if($wrapper.find('#'+whichmceditor).is(':visible')){
content = $wrapper.find('#'+whichmceditor).val();
}else{
content = tinyMCE.editors[whichmceditor].getContent();
}
}
});
}
That's 3 hours of my life wasted, Maybe the documentation could be clearer, maybe it's me, maybe it's maybeline. 那是我一生中浪费的3个小时,也许文档可能更清晰,也许是我,或者也许是热线。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.