How to remove buttons from tinyMCE in wp_editor added via AJAX

Figured it out. After the first editor has loaded on a page, even if you modify the tinymce settings in the array passed to wp_editor(), those settings are NOT passed along to the tinymce instance that is created when the page is first loaded.

Instead you have to use javascript to modify the tinymce instance itself. You can leave out the filters altogether as well as the tinymce element of the wp_editor() settings array. Here’s the updated code that works:

class MyPlugin {
    function MyPlugin() {
        // AJAX action that instantiates the second editor
        add_action( 'wp_ajax_show_my_editor', array( &$this, 'show_my_editor' );

        // enqueue the javascript that displays the editor in a dialog
        add_action( 'admin_enqueue_scripts', array( &$this, 'admin_enqueue_scripts' ) );
    }

    /**
     * Enqueues the javascript/css that displays the editor in a dialog
     */
    function admin_enqueue_scripts() {
        wp_enqueue_script( 'editor_dialog_js', plugins_url( 'js/editor_dialog.js', __FILE__ ), array( 'jquery', 'jquery-ui-dialog' ), false, true );
        wp_enqueue_style( 'jquery_ui_smoothness', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/themes/smoothness/jquery-ui.css' );
    }

    /**
     * Instantiates a wp_editor instance via AJAX call
     */
    function show_my_editor() {
        wp_editor( '', 'myeditor', array(
            'media_buttons' => false,
            'textarea_rows' => 5,
            'quicktags'     => true
        ));
        exit;
    }

} // end MyPlugin class

And then the editor_dialog.js file:

jQuery( document ).ready( function( $ ) {
    $( '#show_editor_dialog_button' ).click( function() {

        // ADD THIS LINE TO MODIFY THE BUTTONS DIRECTLY THROUGH TINYMCE
        tinymce.settings.theme_advanced_buttons1 = 'bold,italic,ppadlinkto';

        $.get( ajaxurl, { action: 'show_my_editor' } )
        .success( function( editor ) {
            $( '<div></div>' ).html( editor )
            .dialog({
                modal: true,
                buttons: { 'OK': function() { $( this ).dialog( 'close' ); } },
                width: 500
            });
            tinymce.execCommand( 'mceAddControl', true, 'myeditor' );
            quicktags( { id: 'myeditor' } );
        });
    });
});

It’s nice when the solution shortens your code that much! Just wish there were better documentation…. 🙂