通过Ajax插件加载Wordpress编辑器
Load Wordpress Editor Via Ajax Plugin
我在stackoverflow上研究了几个关于这个主题的类似问题,这些问题已经有了答案。
有些答案似乎并没有完全奏效,有些只是我的想法。
一个多星期以来,我一直在阅读和修改我的代码,所以我想我会尝试再次提出比其他问题更详细的问题。
我写了一个非常简单的WordPress插件,它的唯一目的就是通过ajax加载一个功能齐全的编辑器。
以下是该插件工作的屏幕截图(有错误):http://screencast.com/t/eyrTdbUy
我想如果我的问题能够得到回答,将会帮助很多人。
以下正是插件的作用。
-
它加载我的自定义页面模板,而不是主题模板。在这个模板中,有一个使用wp_editor函数创建的编辑器(用于加载所需的文件)和一个添加新编辑器的链接。
-
当您单击"添加编辑器"链接时,将通过ajax使用wp_editor函数创建一个新编辑器,然后使用javascript初始化,并添加新链接以添加另一个编辑器。
这只适用于用户登录的情况。
我不建议在你的活动网站上安装这个,因为它会占据你的页面。这个插件只是一个例子,所以它应该只安装在测试网站上。
以下是问题
- ajax加载编辑器的第一个实例可以工作,但当您单击选项卡从视觉到文本来回切换时,会出现以下错误"TypeError:e未定义"TypeError:c未定义"
当加载第一个新编辑器时,也会发生"TypeError:e is undefined"。
- 加载第一个实例后,无法添加另一个编辑器
所以我的问题是…我的代码出了什么问题?
该插件由4个文件组成。
文件1是插件文件"load_editor.php"(它只包括函数):
include('functions.php');
文件2是函数文件"functions.php":
<?
// load custom editor template
function load_editor_template( $template )
{
$template = plugin_dir_path( __FILE__ ) . 'template.php';
return $template;
}
add_filter( 'template_include', 'load_editor_template' );
// load javascript
function load_editor_scripts() {
wp_enqueue_script( 'load_editor', plugins_url() . '/load_editor/js/load_editor.js', array(), '', true );
wp_enqueue_script( 'jquery');
}
add_action( 'wp_enqueue_scripts', 'load_editor_scripts' );
// create new editor
function load_editor_new_editor() {
$id = $_POST['id'];
$number = $_POST['number'];
$next = $number + 1;
$full_id = $id.$number;
echo "<h1>Editor $number</h1>";
$content = '<p>This is example content.</p>';
wp_editor($content, $full_id, array('textarea_rows' => 3));
// initiate new editor
echo "<script>
tinyMCE.execCommand('mceAddEditor', true, $full_id);
tinyMCE.init(tinyMCEPreInit.mceInit[$full_id]);
</script>";
// create "add new" text
echo "<div><a onclick='"load_new_editor('editor', $next);'" href='javascript:void(0);'>Click here</a> to add another editor</div>";
die();
}
add_action( 'wp_ajax_load_editor_new_editor', 'load_editor_new_editor' );
文件3是模板文件"template.php":
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Load Editor</title>
<?php wp_head(); ?>
</head>
<body>
<? wp_editor('Ecample content', 'id', array('textarea_rows' => 3)); ?>
<div id="add"><a onClick="load_new_editor('editor', 1);" href="javascript:void(0);">Click here</a> to add an editor</div>
<div id="editor_container">
<!-- Editors will load here -->
</div>
<script>
<?
echo 'ajaxurl = "'.admin_url('admin-ajax.php').'";';
?>
</script>
<?php wp_footer(); ?>
</body>
</html>
文件4是javascript文件"load_editor.js":
function load_new_editor(id, number){
// remove click to add
jQuery('#add').remove();
var fullId = id + number;
var data = {
'action': 'load_editor_new_editor',
'number': number,
'id': id
};
jQuery.post(ajaxurl, data, function(response) {
//add new editor
jQuery('#editor_container').append(response);
});
}
我也把它放在github上:在此处输入链接描述
非常感谢你能提供的任何帮助。我一直在努力让它发挥作用这么久了,这让我很头疼。我甚至通过elance雇佣了一名程序员,但他无法像我那样走得更远。
这是我能想到的最好的,我认为它对我来说已经足够好了。
除了快速标签,一切都在工作,没有它们我也能生活。
我从funsions.php中删除了javascript
<?
// load custom editor template
function load_editor_template( $template )
{
$template = plugin_dir_path( __FILE__ ) . 'template.php';
return $template;
}
add_filter( 'template_include', 'load_editor_template' );
// load javascript
function load_editor_scripts() {
wp_enqueue_script( 'load_editor', plugins_url() . '/load_editor/js/load_editor.js', array(), '', true );
wp_enqueue_script( 'jquery');
}
add_action( 'wp_enqueue_scripts', 'load_editor_scripts' );
// create new editor
function load_editor_new_editor() {
$id = $_POST['id'];
$number = $_POST['number'];
$next = $number + 1;
$full_id = $id.$number;
echo "<h1>Editor $number</h1>";
$content = '<p>This is example content.</p>';
wp_editor($content, $full_id, array('textarea_rows' => 3));
// create "add new" text
echo "<div id='add'><a onclick='"load_new_editor('editor', $next);'" href='javascript:void(0);'>Click here</a> to add another editor</div>";
die();
}
add_action( 'wp_ajax_load_editor_new_editor', 'load_editor_new_editor' );
然后我在load_editor.js上更改了以下内容
- 添加了快速标签功能,使标签可以正常工作而不会出错
- 使用WordPress使用的设置调用tinymce.init
我想就是这样。
// JavaScript Document
function load_new_editor(id, number){
// remove click to add
jQuery('#add').remove();
var fullId = id + number;
var data = {
'action': 'load_editor_new_editor',
'number': number,
'id': id
};
jQuery.post(ajaxurl, data, function(response) {
//add new editor
jQuery('#editor_container').append(response);
// this is need for the tabs to work
quicktags({id : fullId});
// use wordpress settings
tinymce.init({
selector: fullId,
theme:"modern",
skin:"lightgray",
language:"en",
formats:{
alignleft: [
{selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles: {textAlign:'left'}},
{selector: 'img,table,dl.wp-caption', classes: 'alignleft'}
],
aligncenter: [
{selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles: {textAlign:'center'}},
{selector: 'img,table,dl.wp-caption', classes: 'aligncenter'}
],
alignright: [
{selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles: {textAlign:'right'}},
{selector: 'img,table,dl.wp-caption', classes: 'alignright'}
],
strikethrough: {inline: 'del'}
},
relative_urls:false,
remove_script_host:false,
convert_urls:false,
browser_spellcheck:true,
fix_list_elements:true,
entities:"38,amp,60,lt,62,gt",
entity_encoding:"raw",
keep_styles:false,
paste_webkit_styles:"font-weight font-style color",
preview_styles:"font-family font-size font-weight font-style text-decoration text-transform",
wpeditimage_disable_captions:false,
wpeditimage_html5_captions:true,
plugins:"charmap,hr,media,paste,tabfocus,textcolor,fullscreen,wordpress,wpeditimage,wpgallery,wplink,wpdialogs,wpview",
selector:"#" + fullId,
resize:"vertical",
menubar:false,
wpautop:true,
indent:false,
toolbar1:"bold,italic,strikethrough,bullist,numlist,blockquote,hr,alignleft,aligncenter,alignright,link,unlink,wp_more,spellchecker,fullscreen,wp_adv",toolbar2:"formatselect,underline,alignjustify,forecolor,pastetext,removeformat,charmap,outdent,indent,undo,redo,wp_help",
toolbar3:"",
toolbar4:"",
tabfocus_elements:":prev,:next",
body_class:"id post-type-post post-status-publish post-format-standard",
});
// this is needed for the editor to initiate
tinyMCE.execCommand('mceAddEditor', false, fullId);
});
}
这是它工作的屏幕截图。http://screencast.com/t/Psd9IVVY
如果有人知道如何让快速标签显示出来,我很想知道。
此外,如果你发现我的代码有问题,请告诉我。
如果你想在这里下载,我已经更新了github:https://github.com/ccbgs/load_editor
(答案如下:https://wordpress.stackexchange.com/questions/51776/how-to-load-wp-editor-through-ajax-jquery/192132)
1) 在functions.php中,添加:
add_action('init','my_wpEditOUPUTT'); function my_wpEditOUPUTT(){
if (isset($_POST['Give_me_editorrr'])){
wp_editor( '' , 'txtrID_'.$_POST['myNumber'], $settings = array( 'editor_class'=>'my_class', 'textarea_name'=>'named_'. $_POST['myNumber'], 'tinymce'=>true , 'media_buttons' => true , 'teeny' => false,));
exit;
}
}
2) 仪表板内部HTML页面:
<div id="MyPlace"></div> <a href="javascript:myLoad();">Click to load</a>
<script type="text/javascript">
startNumber = 1;
function myLoad(){ alert('wait 1 sec');
startNumber ++;
jQuery.post('./index.php', '&Give_me_editorrr=1&myNumber='+startNumber ,
function(data,status){
if (status == "success") {
document.getElementById('MyPlace').innerHTML += data; alert("Inserted!");
tinymce.init({ selector: 'txtrID_'+startNumber, theme:'modern', skin:'lightgray'}); tinyMCE.execCommand('mceAddEditor', false, 'txtrID_'+startNumber);
}
});
}
</script>
- ExtJS网格单元格编辑器,防止焦点松动问题
- 可以't使用PHP使用Froala编辑器上传图像URL
- 如何在corona sdk中从CK编辑器中检索数据
- Eclipse编辑器中无法访问的代码
- 如何将拼写检查集成到密文编辑器
- 一个页面上有多个Ace编辑器,没有预先设置元素
- 小部件中的 WordPress 核心颜色选择器 (iris) - 在 WordPress 定制器中编辑时刷新
- wordpress页面编辑器中的javascript不起作用
- 正在寻找类似wordpress的视觉编辑器
- 通过Ajax插件加载Wordpress编辑器
- 在WordPress编辑器中突出显示或下划线搜索词
- 我如何用一个插件添加多个按钮到我的WordPress帖子编辑器
- 在WordPress文本区元框中添加wysiwyg编辑器
- 自定义WordPress管理菜单:如何使编辑器可编辑
- 添加编辑器到Wordpress主题页面
- 动态CSS与PHP - Wordpress &视觉编辑器按钮
- Wordpress将javascript添加到原生Wordpress帖子/页面编辑器中
- 在帖子的可视化编辑器中删除了Wordpress javascript参数
- 表单中需要的Wordpress文本编辑器字段
- WordPress - 在编辑器中插入html /文本