Add multiple Visual Editors for Custom Post Type

That can be achieved with the hooks edit_form_after_title and edit_form_after_editor and proceed like a meta box. I noticed a “glitch”, though, if you swap Visual/Html in the custom wp_editors and then publish/refresh the page, their state will be the same as the main editor (the post content).

Adjust the post type, page in this example.

add_action( 'edit_form_after_editor', 'no_metabox_wspe_114084' );
add_action( 'save_post', 'save_wpse_114084', 10, 2 );

function no_metabox_wspe_114084()
{
    global $post;
    if( 'page' != $post->post_type )
        return;

    $editor1 = get_post_meta( $post->ID, '_custom_editor_1', true);
    $editor2 = get_post_meta( $post->ID, '_custom_editor_2', true);

    wp_nonce_field( plugin_basename( __FILE__ ), 'wspe_114084' );
    echo '<h2>Aux editor 1</h2>';
    echo wp_editor( $editor1, 'custom_editor_1', array( 'textarea_name' => 'custom_editor_1' ) );
    echo '<h2>Aux editor 2</h2>';
    echo wp_editor( $editor2, 'custom_editor_2', array( 'textarea_name' => 'custom_editor_2' ) );
}

function save_wpse_114084( $post_id, $post_object )
{
    if( !isset( $post_object->post_type ) || 'page' != $post_object->post_type )
        return;

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return;

    if ( !isset( $_POST['wspe_114084'] ) || !wp_verify_nonce( $_POST['wspe_114084'], plugin_basename( __FILE__ ) ) )
        return;

    if ( isset( $_POST['custom_editor_1'] )  )
        update_post_meta( $post_id, '_custom_editor_1', $_POST['custom_editor_1'] );

    if ( isset( $_POST['custom_editor_2'] )  )
        update_post_meta( $post_id, '_custom_editor_2', $_POST['custom_editor_2'] );
}

Leave a Comment