Reputation: 73
I made a group of fields of type Checkbox to be able to select several options using ACF | Advanced Custom Fields, these fields are added to the profile of the user in the WP dashboard, I want to edit them from the front end using a custom form.
I need when you select any option / checkbox on the page of the frontpage that is saved and also updated in the user's profile.
The code that I have in the from template is this: account_page.php
<form id="your-profile" class="nice" action="<?php echo the_permalink() ?>" method="POST" />
<div>
<p>Please check the email newsletters you wish to receive.<br>Unchecking a box will unsubscribe you from future emails.</p>
</div>
<fieldset>
<input name="_wp_http_referer" value="<?php echo home_url('account') ?>" type="hidden">
<input name="from" value="profile" type="hidden">
<input name="action" value="update" type="hidden">
<input name="user_id" id="user_id" value="<?php echo $current_user->ID ?>" type="hidden">
<input name="checkuser_id" value="<?php echo get_current_user_id(); ?>" type="hidden">
<input name="nickname" value="<?php echo $current_user->user_email ?>" type="hidden">
<input name="user_email" value="<?php echo $current_user->user_email ?>" type="hidden">
<input name="phone" value="<?php echo $current_user->phone; ?>" type="hidden">
<input name="first_name" value="<?php echo $current_user->first_name ?>" type="hidden">
<input name="last_name" value="<?php echo $current_user->last_name ?>" type="hidden">
<input name="address" value="<?php echo $current_user->address ?>" type="hidden">
<input name="address2" value="<?php echo $current_user->address2 ?>" type="hidden">
<input name="city" value="<?php echo $current_user->city; ?>" type="hidden">
<input name="state" value="<?php echo $current_user->state; ?>" type="hidden">
<input name="zip_code" value="<?php echo $current_user->zip_code; ?>" type="hidden">
<input name="country" value="<?php echo $current_user->country; ?>" type="hidden">
<?php
//Get all items from newslwtter
$newsletter = get_field('newsletter_preferences', $current_user);
$id = get_the_ID();
$field = get_field_objects($current_user);
$field_name = $field["newsletter_preferences"]["name"];
if( $field ){
foreach ( $field["newsletter_preferences"]['choices'] as $key => $value) {
if ( in_array( $key, $newsletter ) ) {
$checked = 'checked';
}else{
$checked = '';
}
echo '<p class="field"><input type="checkbox" '.$checked.' name="'.$field_name.'" value="'.$key.'">'.$value.'</p>';
}
}
?>
</fieldset>
<div>
<input class="btn btn-primary" name="Update E-mail Preferences" value="Update E-mail Preferences" type="submit">
<?php wp_nonce_field( 'update-user' ) ?>
<input name="action" type="hidden" id="action" value="update-user">
</div>
</form>
And in the functions.php
function update_extra_profile_fields($user_id) {
if ( isset( $_POST['newsletter_preferences'] ) )
update_user_meta( $user_id, 'newsletter_preferences',
$_POST['newsletter_preferences'] );
}
add_action('personal_options_update','update_extra_profile_fields');
But don't work, dont save anything, help me please. Thank you
Upvotes: 0
Views: 4118
Reputation: 73
Solved creating :
function update_extra_profile_fields($user_id) {
if ( isset( $_POST['state'] ) )
$state = update_user_meta( $user_id, 'state', $_POST['state'] );
.
.
.
//all fields to save
}
add_action('personal_options_update', __NAMESPACE__ . '\\update_extra_profile_fields');
Upvotes: 0