Metabox saving values

Question

I have 2 metaboxes (“prices” and “dates”) on my custom post type (“kvkoolitus”) edit page.
As I thought right, wordpress saves values only from first rendered metabox.
Each metabox code stored in their own files. If I include only 1 metabox – it works, if both are included – wordpress saves values only from first rendered metabox.
What did I miss?

First metabox creation function:

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' );

Second metabox creation function:

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' );

First metabox callback function:

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 }

Second metabox callback function:

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 }

First metabox save function:

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');

Second metabox save function:

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');

--------------------------------------------------

>>> Share a link to this question, just copy and paste the code bellow on your web page <<<

<a href="https://www.wptricks.com/question/metabox-saving-values/">

Metabox saving values</a>
0
, , anton 2 months 0 Answers 75 views 0

Leave an answer