动态加载wordpress wp_editor(ajax) [英] load wordpress wp_editor dynamically (ajax)

查看:166
本文介绍了动态加载wordpress wp_editor(ajax)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个答案/解决方案,而不是一个问题,仍然可能存在一些错误,即使我尝试了我的开发环境。

This is an answer/solution rather than a question, still there maybe some bugs, even I tried on my dev env.

我最近尝试使用wp_editor小部件/菜单,经过一些搜索后,我没有找到我想要的完整解决方案。

I recently try to use wp_editor in widget/menu, after some search, I did not find a complete solution as I want.

在我按小时挖掘wp的代码后,我会在下面分享我的解决方案:

I would share my solution in below after I dig into wp's code by hours:

推荐答案

可能涉及黑客攻击,但我试图将它们最小化。

There maybe hacking involved, however, I tried to minimize them.

要使wp_editor可以在动态html中工作(这意味着没有重新加载页面,js会更改页面结构),有两个主要问题需要注意:

To make wp_editor can work in dynamic html (which means without reload page, js changes the page structure), there are two major issues need to take care:


  1. tinymce

  2. qucik-tags

对于[tinymce]:

For [tinymce]:

a。需要正确重置UI

a. need reset UI properly


  • 解决方法是[删除mce实例] - > [获取正确的mce设置] - > [重新初始化一个新的mce实例]

  • solution is [remove mce instance] -> [get proper mce settings] -> [re-init a new mce instance]

在js代码中(id表示textarea id):

in js code (id means textarea id):

tinymce.execCommand('mceRemoveEditor', true, id);
var init = tinymce.extend( {}, tinyMCEPreInit.mceInit[ id ] );
try { tinymce.init( init ); } catch(e){}


b。在提交之前需要数据写回textarea

b. need data write back to textarea before submit


  • 解决方案是[绑定点击按钮] - >关于submt :: [关闭mce] - > [开启提交]

  • 在js代码中:

  • solution is [bind click to button] -> on submt :: [turn off mce] -> [turn on submit]
  • in js code:

jq('textarea[id="' + id + '"]').closest('form').find('input[type="submit"]').click(function(){
    if( getUserSetting( 'editor' ) == 'tmce' ){
        var id = mce.find( 'textarea' ).attr( 'id' );
        tinymce.execCommand( 'mceRemoveEditor', false, id );
        tinymce.execCommand( 'mceAddEditor', false, id );
    }
    return true;
});


对于[快速标签]:

a。重新初始化标签


  • [获取设置] - > [设置鼠标事件] - > [重新初始化QTags]

b。切换到正确的选项卡(mce选项卡或快速标签选项卡)

b. Switch to proper tab (mce tab or quick tag tab)


  • [切换到当前选项卡模式]

  • [switch to current tab mode]

以上均为js代码:

if ( typeof(QTags) == 'function' ) {
    jq( '[id="wp-' + id + '-wrap"]' ).unbind( 'onmousedown' );
    jq( '[id="wp-' + id + '-wrap"]' ).bind( 'onmousedown', function(){
        wpActiveEditor = id;
    });
    QTags( tinyMCEPreInit.qtInit[ id ] );
    QTags._buttonsInit();
    switchEditors.switchto( jq( 'textarea[id="' + id + '"]' ).closest( '.widget-mce' ).find( '.wp-switch-editor.switch-' + ( getUserSetting( 'editor' ) == 'html' ? 'html' : 'tmce' ) )[0] );
}


另外,请记住如果你使用ajax,每次回发mce UI时,你需要在你的js中重新执行[reset mce UI]和[Qtags]。
一个简单的解决方案是使用js代码发回html,并在php中检测:

Also, please remember if you use ajax, every time post back mce UI, you need re-do [reset mce UI] and [Qtags] in you js. A easy solution is using js code in you post back html, and detect in php of:

$ isAjax = defined(' DOING_AJAX')&& DOING_AJAX == true);

关于js值的默认设置:

About default settings in js value:


  1. mce: tinyMCEPreInit.mceInit

qtags: tinyMCEPreInit.qtInit

如果您尝试使用窗口小部件的默认设置模式,您需要找到默认设置。

If you try to use default setting for widget mode, you need locate default settings.

要获取小部件模板ID,请使用js代码:

To get widget template id, in js code:

function getTemplateWidgetId( id ){
        var form = jQuery( 'textarea[id="' + id + '"]' ).closest( 'form' );
        var id_base = form.find( 'input[name="id_base"]' ).val();
        var widget_id = form.find( 'input[name="widget-id"]' ).val();
        return id.replace( widget_id, id_base + '-__i__' );
}

所以你可以通过以下方式获得设置:

So you can get settings by:


  1. 对于mce:

  1. for mce:

var init;
if( typeof tinyMCEPreInit.mceInit[ id ] == 'undefined' ){
    init = tinyMCEPreInit.mceInit[ id ] = tinymce.extend( {}, tinyMCEPreInit.mceInit[ getTemplateWidgetId( id ) ] );
}else{
    init = tinyMCEPreInit.mceInit[ id ];
}


  • 对于Qtags:

  • For Qtags:

    var qInit;
    if( typeof tinyMCEPreInit.qtInit[ id ] == 'undefined' ){
        qInit = tinyMCEPreInit.qtInit[ id ] = jq.extend( {}, tinyMCEPreInit.qtInit[ getTemplateWidgetId( id ) ] );
        qInit['id'] = id;
    }else{
        qInit = tinyMCEPreInit.qtInit[ id ];
    }
    


  • 完整代码样本,请检查: https://github.com/hezachary/wordpress-wysiwyg -widget / blob / master / widget_wp_editor.class.php

    For the complete code sample, please check : https://github.com/hezachary/wordpress-wysiwyg-widget/blob/master/widget_wp_editor.class.php

    如果有人想在菜单中使用wp_editor进行管理,原则应该是一样的。

    If anyone want use wp_editor in menu walk for admin, the principle should be the same.

    如果您有任何疑问或更好的解决方案,请发表评论,谢谢。

    If you have any question or better solut please comment, thanks.

    这篇关于动态加载wordpress wp_editor(ajax)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆