The meta box does not save its content, but that content shows up on page. When editing that page, the meta box is empty and its content is lost. Here’s my code:
/*
* Custom meta boxes
*/
add_action( 'add_meta_boxes', 'create_meta_boxes' );
function create_meta_boxes() {
add_meta_box( 'my-meta-box-id', __('Additional information'), 'meta_box_info', 'Post', 'normal', 'low' );
}
// Create meta box: Additional information
function meta_box_info( $post ) {
$values = get_post_custom( $post->ID );
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<textarea name="meta_box_info" id="meta_box_info" style="width: 100%; margin: 6px 0;" /><?php echo $text; ?></textarea>
<?php
}
// Save meta box: Additional information
add_action( 'save_post', 'save_meta_box_info' );
function save_meta_box_info( $post_id ) {
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
if( !current_user_can( 'edit_post' ) ) return;
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchords can only have href attribute
)
);
if( isset( $_POST['meta_box_info'] ) )
update_post_meta( $post_id, 'meta_box_info', wp_kses( $_POST['meta_box_info'], $allowed ) );
$chk = ( isset( $_POST['meta_box_info_rotation'] ) && $_POST['meta_box_info_rotation'] ) ? 'on' : 'off';
update_post_meta( $post_id, 'meta_box_info_rotation', $chk );
}
You need to add this line before Textarea