我正在创建一个 WordPress 插件。它具有通过 AJAX 添加产品时显示编辑器的功能,但编辑器无法正确显示。
用户可以根据需要添加任意数量的产品,因此请记住,wp_editor() 将不止一个
请参考所附截图:
我使用了以下代码:
PHP
public function add_product() {
// Get product id
$prod_id = filter_input(INPUT_POST, 'pid');
// WordPress WYSIWYG Editor
wp_editor("Test Content", "textarea" . $prod_id, array('textarea_name' => 'text'));
wp_die();
}
查询
$('.add-prod').live('click', function () {
var prod_id = $('.prod-id').val();
var data = {
action: 'add_prod',
pid: prod_id
};
$('#update-msg').show();
$.post(ajaxurl, data, function (result) {
$('#the-list').append(result);
$('#update-msg').hide();
});
return false;
});
我使用了来自互联网的解决方案,但它的部分工作不完全。使用了以下代码:
PHP
wp_editor($product->prod_desc, $textarea_id, array('textarea_name' => 'text'));
\_WP_Editors::enqueue_scripts();
print_footer_scripts();
\_WP_Editors::editor_js();
查询
var eid = '#item-list';
switchEditors.go(eid, 'tmce')
quicktags({id: eid});
//init tinymce
tinyMCEPreInit.mceInit[eid]['elements'] = eid;
tinyMCEPreInit.mceInit[eid]['body_class'] = eid;
tinyMCEPreInit.mceInit[eid]['succesful'] = false;
tinymce.init(tinyMCEPreInit.mceInit[eid]);
上面的代码是这样做的: