0

I'm designing a site that will be part of a WP-Network; we have been told we cannot install plugins.

I have a custom post type, which I have added in functions.php of the child-theme I am writing. In addition to the built-in editor that is available as part of the Edit Post, I would like to add a further two visual editors - I believe from 3.3, the widget is called wp_editor.

Is it possible to add these without the use of metaboxes? I have already used WPAlchemy as per this article, but I didn't like that it wasn't possible to switch from Visual to Text, and the fact that the editor appeared inside another box.

I have also seen this question on WPA which appears to be asking the same question, but the answer also uses WPAlchemy.

I have seen how to use the wp_editor on front-end pages, but not how to add them to the back end edit pages for specific post types. Thanks.

1 Answer 1

6

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'] );
}
1
  • Thanks brasofilo, this did the trick. Could you elaborate on the sentence, "Seems that the initial state of the main editor (the content) rules the state of the extra ones" - I am unsure what you mean by this.
    – ataulm
    Commented Sep 13, 2013 at 23:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.