I'm creating a simple plugin that lets me to save some input text fields while the user is registering on the site. This part works well.
But I want to let the users edit this fields on the user account page. I can load the saved info on the database but I can't update this info on the the user metadata.
This code puts the input on the register page, the others 4 possible inputs are dynamically added with jquery if the user clicks on the +
button.
add_action ('register_form', 'save_dynamic_inputs', 10);
function save_dynamic_inputs() {
$user_cnpj = ( isset( $_POST['user_cnpj_1'] ) ) ? $_POST['user_cnpj_1'] : '';
$user_cnpj = ( isset( $_POST['user_cnpj_2'] ) ) ? $_POST['user_cnpj_2'] : '';
$user_cnpj = ( isset( $_POST['user_cnpj_3'] ) ) ? $_POST['user_cnpj_3'] : '';
$user_cnpj = ( isset( $_POST['user_cnpj_4'] ) ) ? $_POST['user_cnpj_4'] : '';
?>
<div class="cnpjs"><p><label for="user_cnpj_1"><?php _e( 'CNPJ', 'save_dynamic_inputs' ) ?><br /><input type="text" name="user_cnpj_1" id="user_cnpj_1" class="input" style="width: 445px; value="<?php echo esc_attr( stripslashes( $user_cnpj ) ); ?>" size="19" /><i class="button addmore fa fa-plus" aria-hidden="true"></i></label></p></div>
<?php
}
This another code saves every input if they exist in the user metadata, and this code works, but it's for the register form:
add_action( 'user_register', 'saving_my_dynamic_inputs', 10, 1 );
function saving_my_dynamic_inputs( $user_id ) {
for ($i = 1; $i < 5; $i++) {
if ( isset( $_POST['user_cnpj_'.$i] ) )
update_user_meta($user_id, 'user_cnpj_'.$i, $_POST['user_cnpj_'.$i]);
}
}
And this is the code that I'm using to update the user metadata on the user edit account page, this code does not work.
add_action('woocommerce_created_customer', 'update_fields_profile_user_woo', 10, 1);
function update_fields_profile_user_woo($customer_id) {
if ( isset( $_POST['user_cnpj_1'] ) ) {
update_user_meta($customer_id, 'user_cnpj_1', $_POST['user_cnpj_1']);
} else {
echo "<pre>",print_r($_POST),"</pre>";
exit();
}
}
Where you can read woocommerce_created_customer
, I have tried woocommerce_save_account_details
and others.
I don't understand why the inputs does not update the user metadata. I have tried to point the form to a php file that gets the $_POST var and prints this, and the array have all the inputs like the sample above:
Like I said, the user account profile loads all the inputs needed, but this last code does not update the info on user metadata.
woocommerce_save_account_details
? That looks like the correct hook. – helgatheviking