Metabox saving values

时间:2016-07-08 作者:anton

在我的自定义帖子类型(“kvkoolitus”)编辑页面上有两个元数据库(“价格”和“日期”)。正如我所想的那样,wordpress只保存第一个渲染的元盒中的值。每个元盒代码存储在各自的文件中。如果我只包含1个元盒-它可以工作,如果两者都包含-wordpress只保存第一个渲染的元盒中的值
我错过了什么?

第一个metabox创建函数:

function kvkoolitus_prices_metabox() {
    add_meta_box( 
        \'kvkoolitus_prices_metabox\', 
        __( \'Course Price\', \'kvkoolitus\'), 
        \'kvkoolitus_prices_metabox_callback\', 
        \'kvkoolitus\', 
        \'normal\', 
        \'default\'
    );
}
add_action( \'add_meta_boxes_kvkoolitus\', \'kvkoolitus_prices_metabox\' );
第二个metabox创建函数:

function kvkoolitus_dates_metabox() {
    add_meta_box( 
        \'kvkoolitus_dates_metabox\', 
        __( \'Course Time\', \'kvkoolitus\'), 
        \'kvkoolitus_dates_metabox_callback\', 
        \'kvkoolitus\', 
        \'normal\', 
        \'default\'
    ); 
}
add_action( \'add_meta_boxes_kvkoolitus\', \'kvkoolitus_dates_metabox\' );
第一个metabox回调函数:

function kvkoolitus_dates_metabox_callback( $post ) { 

    wp_nonce_field( \'kvkoolitus_dates_metabox_nonce\', \'kvkoolitus_dates_nonce\' ); ?>

<form action="" method="post">

  <?php         
    $start_date = get_post_meta( $post->ID, \'kvkoolitus-start\', true );
    $end_date   = get_post_meta( $post->ID, \'kvkoolitus-end\', true );
    $duration   = get_post_meta( $post->ID, \'kvkoolitus-duration\', true );
  ?>

  <p>   
    <label for="kvkoolitus_start_date"><?php _e(\'Start date\', \'kvkoolitus\' ); ?></label><br/>    
    <input type="text" class="widefat kvkoolitusDate" name="kvkoolitus_start_date" value="<?php echo esc_attr( $start_date ); ?>" />
  </p>

  <p>
  <label for="kvkoolitus_end_date"><?php _e(\'End date\', \'kvkoolitus\' ); ?></label><br/> 
  <input type="text" class="widefat kvkoolitusDate" name="kvkoolitus_end_date" value="<?php echo esc_attr( $end_date ); ?>" />
  </p>    

  <p>
  <label for="kvkoolitus_duration"><?php _e(\'Duration\', \'kvkoolitus\' ); ?></label><br/> 
  <input type="text" class="widefat" name="kvkoolitus_duration" value="<?php echo esc_attr( $duration ); ?>" />
  </p>

</form>

<?php }
第二个metabox回调函数:

function kvkoolitus_prices_metabox_callback( $post ) { 

wp_nonce_field( \'kvkoolitus_prices_metabox_nonce\', \'kvkoolitus_prices_nonce\' ); ?>

<form action="" method="post">

  <?php         
    $price   = get_post_meta( $post->ID, \'kvkoolitus-price\', true );
    $km      = get_post_meta( $post->ID, \'kvkoolitus-km\', true );
  ?>  

  <p>
  <label for="kvkoolitus_price"><?php _e(\'Price\', \'kvkoolitus\' ); ?></label><br/> 
  <input type="text" class="widefat" name="kvkoolitus_price" value="<?php echo esc_attr( $price ); ?>" />
  </p>   

  <p>
  <input type="checkbox" value="1" name="kvkoolitus_km" <?php checked( \'1\', $km ); ?> />
  <label for="kvkoolitus_km"><?php _e(\'Income Tax Included\', \'kvkoolitus\' ); ?></label><br/> 
  </p>          

</form>

<?php }
第一个metabox保存功能:

function kvkoolitus_dates_save_meta( $post_id ) {

  if( !isset( $_POST[\'kvkoolitus_dates_nonce\'] ) || !wp_verify_nonce( $_POST[\'kvkoolitus_dates_nonce\'],\'kvkoolitus_dates_metabox_nonce\') ) 
    return;

  if ( !current_user_can( \'edit_post\', $post_id ))
    return;

  if ( isset($_POST[\'kvkoolitus_start_date\']) ) {        
    update_post_meta($post_id, \'kvkoolitus-start\', sanitize_text_field( $_POST[\'kvkoolitus_start_date\']));      
  }  

  if ( isset($_POST[\'kvkoolitus_end_date\']) ) {        
    update_post_meta($post_id, \'kvkoolitus-end\', sanitize_text_field( $_POST[\'kvkoolitus_end_date\'] ));      
  }

  if ( isset($_POST[\'kvkoolitus_duration\']) ) {        
    update_post_meta($post_id, \'kvkoolitus-duration\',  sanitize_text_field($_POST[\'kvkoolitus_duration\']));      
  }

}
add_action(\'save_post\', \'kvkoolitus_dates_save_meta\');
第二个metabox保存功能:

function kvkoolitus_prices_save_meta( $post_id ) {

  if( !isset( $_POST[\'kvkoolitus_prices_nonce\'] ) || !wp_verify_nonce( $_POST[\'kvkoolitus_prices_nonce\'],\'kvkoolitus_prices_metabox_nonce\') ) 
    return;

  if ( !current_user_can( \'edit_post\', $post_id ))
    return;

  if ( isset($_POST[\'kvkoolitus_price\']) ) {        
    update_post_meta($post_id, \'kvkoolitus-price\', sanitize_text_field($_POST[\'kvkoolitus_price\']));      
  }

  if ( isset($_POST[\'kvkoolitus_km\']) ) {        
    update_post_meta($post_id, \'kvkoolitus-km\', sanitize_text_field($_POST[\'kvkoolitus_km\']));      
  }

}
add_action(\'save_post\', \'kvkoolitus_prices_save_meta\');

1 个回复
最合适的回答,由SO网友:cybmeta 整理而成

首先,你不需要<form> 标签;元框已在编辑后表单中呈现。

其次,如果未选中复选框,则不会发送该复选框,因此isset( $_POST[\'checkbox_name\'] ) 回来false 并且存储复选框值的自定义字段未正确更新。

我已经用standard post 如果将其与自定义帖子类型一起使用,则它也必须有效:

function kvkoolitus_prices_metabox() {
    add_meta_box( 
        \'kvkoolitus_prices_metabox\', 
        __( \'Course Price\', \'kvkoolitus\'), 
        \'kvkoolitus_prices_metabox_callback\', 
        \'post\', 
        \'normal\', 
        \'default\'
    );
}
add_action( \'add_meta_boxes\', \'kvkoolitus_prices_metabox\' );

function kvkoolitus_dates_metabox() {
    add_meta_box( 
        \'kvkoolitus_dates_metabox\', 
        __( \'Course Time\', \'kvkoolitus\'), 
        \'kvkoolitus_dates_metabox_callback\', 
        \'post\', 
        \'normal\', 
        \'default\'
    ); 
}
add_action( \'add_meta_boxes\', \'kvkoolitus_dates_metabox\' );

function kvkoolitus_dates_metabox_callback( $post ) { 

    wp_nonce_field( \'kvkoolitus_dates_metabox_nonce\', \'kvkoolitus_dates_nonce\' ); ?>

  <?php         
    $start_date = get_post_meta( $post->ID, \'kvkoolitus-start\', true );
    $end_date   = get_post_meta( $post->ID, \'kvkoolitus-end\', true );
    $duration   = get_post_meta( $post->ID, \'kvkoolitus-duration\', true );
  ?>

  <p>   
    <label for="kvkoolitus_start_date"><?php _e(\'Start date\', \'kvkoolitus\' ); ?></label><br/>    
    <input type="text" class="widefat kvkoolitusDate" name="kvkoolitus_start_date" value="<?php echo esc_attr( $start_date ); ?>" />
  </p>

  <p>
  <label for="kvkoolitus_end_date"><?php _e(\'End date\', \'kvkoolitus\' ); ?></label><br/> 
  <input type="text" class="widefat kvkoolitusDate" name="kvkoolitus_end_date" value="<?php echo esc_attr( $end_date ); ?>" />
  </p>    

  <p>
  <label for="kvkoolitus_duration"><?php _e(\'Duration\', \'kvkoolitus\' ); ?></label><br/> 
  <input type="text" class="widefat" name="kvkoolitus_duration" value="<?php echo esc_attr( $duration ); ?>" />
  </p>

<?php }

function kvkoolitus_prices_metabox_callback( $post ) { 

wp_nonce_field( \'kvkoolitus_prices_metabox_nonce\', \'kvkoolitus_prices_nonce\' ); ?>

  <?php         
    $price   = get_post_meta( $post->ID, \'kvkoolitus-price\', true );
    $km      = get_post_meta( $post->ID, \'kvkoolitus-km\', true );
  ?>

  <p>
  <label for="kvkoolitus_price"><?php _e(\'Price\', \'kvkoolitus\' ); ?></label><br/> 
  <input type="text" class="widefat" name="kvkoolitus_price" value="<?php echo esc_attr( $price ); ?>" />
  </p>   

  <p>
  <input type="checkbox" value="1" name="kvkoolitus_km" <?php checked( \'1\', $km ); ?> />
  <label for="kvkoolitus_km"><?php _e(\'Income Tax Included\', \'kvkoolitus\' ); ?></label><br/> 
  </p>  

<?php }

function kvkoolitus_dates_save_meta( $post_id ) {

  if( !isset( $_POST[\'kvkoolitus_dates_nonce\'] ) || !wp_verify_nonce( $_POST[\'kvkoolitus_dates_nonce\'],\'kvkoolitus_dates_metabox_nonce\') ) 
    return;

  if ( !current_user_can( \'edit_post\', $post_id ))
    return;

  if ( isset($_POST[\'kvkoolitus_start_date\']) ) {        
    update_post_meta($post_id, \'kvkoolitus-start\', sanitize_text_field( $_POST[\'kvkoolitus_start_date\']));      
  }  

  if ( isset($_POST[\'kvkoolitus_end_date\']) ) {        
    update_post_meta($post_id, \'kvkoolitus-end\', sanitize_text_field( $_POST[\'kvkoolitus_end_date\'] ));      
  }

  if ( isset($_POST[\'kvkoolitus_duration\']) ) {        
    update_post_meta($post_id, \'kvkoolitus-duration\',  sanitize_text_field($_POST[\'kvkoolitus_duration\']));      
  }

}
add_action(\'save_post\', \'kvkoolitus_dates_save_meta\');

function kvkoolitus_prices_save_meta( $post_id ) {

  if( !isset( $_POST[\'kvkoolitus_prices_nonce\'] ) || !wp_verify_nonce( $_POST[\'kvkoolitus_prices_nonce\'],\'kvkoolitus_prices_metabox_nonce\') ) 
    return;

  if ( !current_user_can( \'edit_post\', $post_id ))
    return;

  if ( isset($_POST[\'kvkoolitus_price\']) ) {        
    update_post_meta($post_id, \'kvkoolitus-price\', sanitize_text_field($_POST[\'kvkoolitus_price\']));      
  }

  if ( isset($_POST[\'kvkoolitus_km\']) ) {        
    update_post_meta($post_id, \'kvkoolitus-km\', sanitize_text_field($_POST[\'kvkoolitus_km\']));      
  } else {
    delete_post_meta($post_id, \'kvkoolitus-km\');
  }

}
add_action(\'save_post\', \'kvkoolitus_prices_save_meta\');

相关推荐

如何在WordPress开发中添加带有ACF自定义字段ID的自定义metabox字段

我是wordpress开发的新手,我在我的项目中安装了高级自定义字段插件,并创建了两个文本字段名称&;我还创建了一个插件,可以在帖子中创建一个带有文本框的元框。现在在帖子中,我将获得自定义字段名称(&A);电子邮件和我的自定义元框旁边将出现,但我必须将我的元框附加到名称字段旁边,即在名称字段和电子邮件字段之间。我的metabox代码如下。请任何人帮帮我//Creating the custom meta box function my_notice_meta_box() {