Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End WordPress: Add a 2nd TinyMCE Meta Box Re: WordPress: Add a 2nd TinyMCE Meta Box

#83242
TheDoc
Member

I absolutely poured hours into this and finally, after much frustration, have a working system. This is what goes in functions.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, $meta_box, 'mytheme_show_box', $meta_box, $meta_box, $meta_box);
}

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

// Use nonce for verification
echo '';

foreach ($meta_box as $field) {
// get current post meta data

$meta = get_post_meta($post->ID, $field, true);

echo '', '
', $field;

}
}

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, basename(__FILE__))) {
return $post_id;
}

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

// check permissions
if ('page' == $_POST) {
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 as $field) {
$old = get_post_meta($post_id, $field, true);
$new = $_POST[$field];

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

?>

And this is what I use to call it on the page:


$meta = get_post_meta(get_the_ID(), 'dbt_textarea', true);
echo $meta; // if you want to show
?>