Adding wp_editor inside a thickbox

I would like to add a wp_editor inside a modal dialog (thickbox) at frontend but only gets an empty editor with no buttons on in. (WordPress 3.5 and plugin development)

The wp_editor is attached via an ajax call…

Read More

It seems like some important javascripts is missing to the page.

Is it possible to preload editor scripts i WP in some way?

Here is a sample plugin to expose the problem (adds a edit link to every content):

<?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);

    }

}
?>

UPDATE

Thanks to @danielauener one solution is to initialize mceAddControl. So here is a working sample of the same plugin:

<?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);

    }

}
?>

It would be really nice to skip the ugly wp_editor initialization 🙂

Related posts

Leave a Reply

1 comment

  1. Funny, that thickbox doesn’t provides any callbacks. But as I mentioned on twitter, I would try a $.ajaxSuccess call to solve the ugly js-inject. Just add the following to one of your javascript files:

    (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 );
    

    UPDATE: The ajaxSuccess method has to be assigned to an element, changed the code