Meta-box does not preserve value on update
-
I have created google map meta-box for posts and I’m able to insert new values of the desired location. However, when I update the post I have to update the map as well or I will have an empty input.
// /** // * Save the metabox data // */ function wpt_save_events_meta( $post_id, $post ) { // // Return if the user doesn't have edit permissions. if ( ! current_user_can( 'edit_post', $post_id ) ) { return $post_id; } // // Verify this came from the our screen and with proper authorization, // // because save_post can be triggered at other times. if ( ! isset( $_POST['lat'] ) || ! isset( $_POST['lng'] ) || ! wp_verify_nonce( $_POST['map_fields'], basename(__FILE__) ) ) { return $post_id; } // // Now that we're authenticated, time to save the data. // // This sanitizes the data from the field and saves it into an array $events_meta. $events_meta['lat'] = esc_textarea( $_POST['lat'] ); $events_meta['lng'] = esc_textarea( $_POST['lng'] ); $lat = esc_textarea( $_POST['lat'] ); $lng = esc_textarea( $_POST['lng'] ); // // Cycle through the $events_meta array. // // Note, in this example we just have one item, but this is helpful if you have multiple. foreach ( $events_meta as $key => $value ) : // Don't store custom data twice if ( 'revision' === $post->post_type ) { return; } if ( get_post_meta( $post_id, $key, false ) ) { // If the custom field already has a value, update it. update_post_meta( $post_id, $key, $value ); } else { // If the custom field doesn't have a value, add it. add_post_meta( $post_id, $key, $value); } if ( ! $value ) { // Delete the meta key if there's no value delete_post_meta( $post_id, $key ); } endforeach; } add_action( 'save_post', 'wpt_save_events_meta', 1, 2 );
Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
- The topic ‘Meta-box does not preserve value on update’ is closed to new replies.