在thickbox中添加wp_editor

时间:2012-12-30 09:46:27

标签: ajax editor wordpress-plugin thickbox

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

wp_editor通过ajax调用附加...

页面上似乎缺少一些重要的javascripts。

是否有可能以某种方式预加载编辑器脚本?

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

<?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初始化会非常好: - )

1 个答案:

答案 0 :(得分:1)

有趣的是,该thickbox不提供任何回调。但正如我在twitter上提到的,我会尝试$.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方法分配给元素,更改代码