这将适用于管理页面。
要通过JS AJAX将新的wp编辑器附加到容器中,请执行以下操作:
1) 在函数中创建wp\\u ajax函数。php返回wp\\u编辑器
2) 创建jQuery脚本以请求新的文本编辑器并将其附加到容器中,在这种情况下,在按下按钮时
PHP文件
function yourprefix_get_text_editor() {
$content = ""; // Empty because is a new editor
$editor_id = $_POST["text_editor_id"]; // Random ID from AJAX JS call
$textarea_name = $_POST["textarea_name"]; // Name from JS file
$settings = array(
\'textarea_name\' => $textarea_name
);
wp_editor($content, $editor_id, $settings);
wp_die(); // Mandatory wp_die
}
add_action(\'wp_ajax_yourprefix_get_text_editor\', \'yourprefix_get_text_editor\');
JS脚本(jsfile.JS)
jQuery(function($) {
$("someelement").click(function(e) { // To Add an Editor from a button click
var target = themeajax.ajax_url; // Passed from wp_localize_script
var editor_id = "editorid"; // Generate this dynamically
var textarea_name = "textareaname" // Generate this as you need
var data = {
\'action\': \'yourprefix_get_text_editor\',
\'text_editor_id\': editor_id,
\'textarea_name\': textarea_name
}
$.post(target, data, function(response) {
container.append(response); // Use your logic to get the container where you want to append the editor
tinymce.execCommand(\'mceAddEditor\', false, editor_id);
quicktags({id : editor_id});
});
}
});
排队脚本调用:
function yourprefix_admin_scripts($hook) {
wp_enqueue_script(\'your-slug\', get_stylesheet_directory_uri() . \'/path/to/jsfile.js\', array(\'jquery\'), null, true);
wp_localize_script(\'your-slug\', \'themeajax\', array(
\'ajax_url\' => admin_url(\'admin-ajax.php\')
));
}
add_action(\'admin_enqueue_scripts\', \'yourprefix_admin_scripts\');