1

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.

Laurel
  • 5,965
  • 14
  • 31
  • 57
Mark
  • 731
  • 2
  • 10
  • 29
  • you need to make the question a bit clearer, what is supposed to happen? All we can see is you are saving something and its 12659. Update means overwrite with the new data btw.... – David May 20 '16 at 00:32
  • Hmm I'll try to make it a bit clearer. On the front end I have the custom field it loads the meta value "12659" perfectly but when I try to change the numbers let's say to "56896" and save the updated field it doesn't save it correctly. What it does it overrides: sp_metrics'a:3:{s:15:"ledennummerknvb";s:5:"12659";s:6:"height";s:1:"5";s:6:"weight";s:1:"5";}' And it will change in this example to: sp_metrics '56896' and the output of the field is just 1 number, in this case a "5". – Mark May 20 '16 at 00:37
  • 1
    well thats what it will do, it will overwrite one value with another, so you are replacing `a:3:{s:15:"ledennummerknvb";s:5:"12659";s:6:"height";s:1:"5";s:6:"wei‌​ght";s:1:"5";}` with `56896` if you want to change the original array, you will need to `get_post_meta` again in your save function, update the key you want and save the `array` again. google wordpress update post meta, (or save_post_meta) it will make mention of this on the page and how it deals with arrays. – David May 20 '16 at 00:54
  • Ah yes I've come across those pages while searching in google. And I don't know how to change the that specific array. I do not know how and or where to change the code so it will work and change the right array. Like do i have to add something like this $sp_metrics= array( 'ledennummerknvb' => , 'height' => '', 'wei‌​ght' => '', ); I'm completely in the dark here of what to do haha – Mark May 20 '16 at 01:19
  • Well just add fields that will update width and the height also, and please sanitize your data. You're echoing unsanitized variable, and when you're updating post meta you're also using unsanitized value that you're setting in your DB - [clicky](https://codex.wordpress.org/Validating_Sanitizing_and_Escaping_User_Data) – dingo_d May 20 '16 at 07:41
  • just google php arrays, you'll never learn basic skills if you dont try these things yourself. – David May 20 '16 at 09:05

0 Answers0