这将在管理页面上起作用。
通过JS AJAX将新的wp编辑器附加到容器中:
1)在functions.php中创建wp_ajax函数以返回wp_editor
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');
admin-ajax.php
?如果没有进行功能与您的代码,然后通过调用它admin-ajax.php