I have a custom code to update a custom field from the front end. It loads the data correctly but when I try to change/update the custom field it goes wrong. It updates the meta value, but it has more values.
These are the meta values:
sp_metrics'a:3:{s:15:"ledennummerknvb";s:5:"12659";s:6:"height";s:1:"5";s:6:"weight";s:1:"5";}'
When I try to update “ledennummerknvb” it goes terribly wrong.
Here’s the code I use to create the custom field:
function your_function_name( $form_id, $post_id, $form_settings ) {
$value = '';
if ( $post_id ) {
$metrics = get_post_meta( $post_id, 'sp_metrics', true );
$ledennummerknvb = $metrics['ledennummerknvb'];
}
?>
<div class="wpuf-label">
<label>Ledennummer KNVB</label>
</div>
<div class="wpuf-fields">
<input type="text" name="my_custom_field" value="<?php echo( $ledennummerknvb ); ?>">
</div>
<?php
}
add_action( 'my_brand_new_hook', 'your_function_name', 10, 3 );
function update_my_brand_new_hook( $post_id ) {
if ( isset( $_POST['my_custom_field'] ) ) {
update_post_meta( $post_id, 'sp_metrics', $_POST['my_custom_field'] );
}
}
add_action( 'wpuf_add_post_after_insert', 'update_my_brand_new_hook' );
add_action( 'wpuf_edit_post_after_update', 'update_my_brand_new_hook' );
So when I update the custom field it overwrites every value plus it will give me back a result of just 1 number. For instance now you see at “ledennummerknvb” the numbers are “12659” but when I change or update them it goes wrong.
I hope someone can help and or explain me what I’m doing wrong as I don’t have the knowledge to figure it out.