Meta Box未保存内容



元框不保存其内容,但该内容显示在页面上。编辑该页面时,元框为空,其内容将丢失。这是我的代码:

/*
 * 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 );
}

您需要在Textarea 之前添加此行

<?php $text = get_post_meta($post->ID, 'meta_box_info', true); ?>

最新更新