0



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

First, you don't need the <form> tags; meta boxes are already rendered inside the post edit form.

Second, if a checkbox is not checked it is not sent, so isset( $_POST['checkbox_name'] ) return false and the custom field that stores the value of the checkbox is not update correctly.

I've tested this code with standard post type and it works, it must works also if you use it with your custom post type:

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');
1
  • 1
    Thank you cybmeta! Nothing to add, removing form tag completely solves saving problem. I also used this method for checkboxes for some projects and it worked, but now I see a logic in your words. Thanks again. – anton Jul 8 '16 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.