4

我想在前端的模态对话框(厚框)内添加一个 wp_editor,但只得到一个没有按钮的空编辑器。(WordPress 3.5 和插件开发)

wp_editor 通过 ajax 调用附加...

页面中似乎缺少一些重要的 javascript。

是否可以以某种方式在 WP 中预加载编辑器脚本?

这是一个暴露问题的示例插件(为每个内容添加一个编辑链接):

<?php
/*
Plugin Name: ThickboxEditor
*/

$thickboxeditor = new ThickboxEditor();

class ThickboxEditor{

    function __construct()
    {

        add_action( 'wp_print_styles', array( &$this, 'wp_print_styles' ) );
        add_action( 'init', array( &$this, 'init' ) );
        add_filter( 'the_content', array( &$this, 'the_content' ) );
        add_action( 'wp_ajax_thickboxeditor', array( &$this, 'editor' ) );
        add_action( 'wp_ajax_nopriv_thickboxeditor', array( &$this, 'editor' ) );

    }

    function the_content( $content ){

        $content .= '<a href="' . admin_url( 'admin-ajax.php' ) . '?action=thickboxeditor" class="thickbox" title="Add new note">EDIT THICKBOXEDITOR</a>';

        return $content;

    }

    function editor(){

        wp_editor( $this->postcontent, 'postcontent', array(
            'media_buttons' => false,
            'teeny' => false,
            'textarea_rows' => '7',
            'tinymce' => array( 'plugins' => 'inlinepopups, fullscreen, wordpress, wplink, wpdialogs' )
        ) );

        die(0);

    }

}
?>

更新

感谢@danielauener,一种解决方案是初始化 mceAddControl。因此,这是同一插件的工作示例:

<?php
/*
Plugin Name: ThickboxEditor
*/

$thickboxeditor = new ThickboxEditor();

class ThickboxEditor{

    function __construct()
    {

        add_action( 'wp_print_styles', array( &$this, 'wp_print_styles' ) );
        add_action( 'init', array( &$this, 'init' ) );
        add_filter( 'the_content', array( &$this, 'the_content' ) );
        add_action( 'wp_ajax_thickboxeditor', array( &$this, 'editor' ) );
        add_action( 'wp_ajax_nopriv_thickboxeditor', array( &$this, 'editor' ) );
        add_action( 'wp_footer', array( &$this, 'wp_footer' ) );

    }

    function wp_footer(){

        echo '<!--';
        wp_editor( '', 'invisible_editor_for_initialization' );
        echo '-->';

    }

    function the_content( $content ){

        $content .= '<a href="' . admin_url( 'admin-ajax.php' ) . '?action=thickboxeditor" class="thickbox" title="Add new note">EDIT THICKBOXEDITOR</a>';

        return $content;

    }

    function editor(){

        wp_editor( '', 'postcontent', array(
            'media_buttons' => false,
            'teeny' => false,
            'textarea_rows' => '7',
            'tinymce' => array( 'plugins' => 'inlinepopups, fullscreen, wordpress, wplink, wpdialogs' )
        ) );

        ?>

        <script language="javascript">
            jQuery(document).ready(function(){
                tinymce.execCommand('mceAddControl',true,'postcontent');
            });
        </script>

        <?php

        die(0);

    }

}
?>

跳过丑陋的 wp_editor 初始化真的很好:-)

4

1 回答 1

1

有趣的是,thickbox 不提供任何回调。但正如我在推特上提到的,我会尝试$.ajaxSuccess调用来解决丑陋的 js-inject。只需将以下内容添加到您的 javascript 文件之一:

(function($) {
    // gets called for every successful ajax call
    $(document).ajaxSuccess( function(evt, request, settings) {

        // make sure this call was from your thickbox
        if ($('#your-thickbox-selector').length > 0) {
            tinymce.execCommand('mceAddControl',true,'postcontent');
        }
    }); 
})( jQuery );

更新:ajaxSuccess方法必须分配给一个元素,更改了代码

于 2012-12-30T11:01:58.593 回答