treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] WordPress: Add a 2nd TinyMCE Meta Box

  • I cannot, for the life of me, figure this one out. Every tutorial on the planet is the most convoluted and complicated thing I've ever read.

    What I am trying to do sounds simple in principle, but is proving to be a time-vortex.

    Goal: Have a second editable text area while editing a page in WordPress that utilizes TinyMCE.

    Adding a simple textarea is easy, it's adding the TinyMCE part that seems to have me stumped.

    There are some plugins out there, but they are either too bloated or have stopped functioning properly. I feel like this is something that people must be utilizing all of the time while developing for WordPress - the only reason I haven't done it before is poor documentation!

    I got really close with this link: http://www.ilovecolors.com.ar/tinymce-plugin-textarea-metaboxes-wordpress/comment-page-1/#comment-8414

    But the details that would help me implement it are missing. Any help would be greatly appreciated!
  • I absolutely poured hours into this and finally, after much frustration, have a working system. This is what goes in functions.php:
    <?php
    $prefix = 'dbt_';

    $meta_box = array(
    'id' => 'my-meta-box',
    'title' => 'Related Links',
    'page' => 'page',
    'context' => 'normal',
    'priority' => 'high',
    'fields' => array (
    array(
    'name' => 'Textarea',
    'desc' => 'Enter big text here',
    'id' => $prefix . 'textarea',
    'type' => 'textarea',
    'std' => 'Default value 2'
    ),
    )
    );

    add_action('admin_menu', 'mytheme_add_box');

    // Add meta box
    function mytheme_add_box() {
    global $meta_box;

    add_meta_box($meta_box['id'], $meta_box['title'], 'mytheme_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
    }

    // Callback function to show fields in meta box
    function mytheme_show_box() {
    global $meta_box, $post;

    // Use nonce for verification
    echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';

    foreach ($meta_box['fields'] as $field) {
    // get current post meta data

    $meta = get_post_meta($post->ID, $field['id'], true);

    echo '<textarea class="theEditor" name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '<br />', $field['desc'];

    }
    }

    add_action('save_post', 'mytheme_save_data');

    // Save data from meta box
    function mytheme_save_data($post_id) {
    global $meta_box;

    // verify nonce
    if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
    return $post_id;
    }

    // check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    return $post_id;
    }

    // check permissions
    if ('page' == $_POST['post_type']) {
    if (!current_user_can('edit_page', $post_id)) {
    return $post_id;
    }
    } elseif (!current_user_can('edit_post', $post_id)) {
    return $post_id;
    }

    foreach ($meta_box['fields'] as $field) {
    $old = get_post_meta($post_id, $field['id'], true);
    $new = $_POST[$field['id']];

    if ($new && $new != $old) {
    update_post_meta($post_id, $field['id'], $new);
    } elseif ('' == $new && $old) {
    delete_post_meta($post_id, $field['id'], $old);
    }
    }
    }

    ?>

    And this is what I use to call it on the page:
    <?php 
    $meta = get_post_meta(get_the_ID(), 'dbt_textarea', true);
    echo $meta; // if you want to show
    ?>
  • Good to know thanks!
  • in wordpress 3.3 version wp_editor function is added to save us from these headaches. for your information
    Here is the link