Support

Account

Home Forums Front-end Issues How update custom field checkbox ACF in user profile from front end

Solved

How update custom field checkbox ACF in user profile from front end

  • 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.

    Fields created:
    https://prnt.sc/i1a1se

    Fields in user profile:
    http://prntscr.com/i1a2g7

    Form in front page
    http://prntscr.com/i1a2ru

    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>
                Please check the email newsletters you wish to receive.<br>Unchecking a box will unsubscribe you from future emails.
    
            </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.'
    ';
                        }
                    }
                ?>
            </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

  • i’m having this issue too .. how did you solve it?

  • Hi, @locomo i solved it,

    1) in custom form front end:

     <form id="your-profile" class="nice" action="<?php echo get_edit_profile_url() ?>" method="POST" />
    <?php
    global $current_user;
    							$allCheckbox = get_field('newsletter_preferences','user_'.$current_user->ID);
    							$field_data = get_field_object('your_field_key');
    							$field_key = $field_data['key'];
    							$field = get_field_object($field_key, 'user_'.$current_user->ID);
    						?>
    							<input type="hidden" value="<?php echo 'acf['.$field['key'].']' ?>"/>
    						<?php
    							foreach($field['choices'] as $key => $label){
    								if(is_array($allCheckbox) && in_array($key, $allCheckbox)){
    								    $checked = 'checked';
    								} else {
    								    $checked = '';
    								}
    							echo '<p class="field"><input type="checkbox" '.$checked.' name="acf['.$field['key'].'][]" value="'.$key.'">'.$label.'</p>';
    							}
    						 ?> </form>

    2) in funtions.php

    function update_extra_profile_fields($user_id) {
    	global $current_user;
    $newsletterOptions = isset( $_POST['acf']['your_field_key'] ) ? $_POST['acf']['your_field_key'] : null;
    		$f_id = update_field( 'your_field_key', $newsletterOptions , 'user_'.$user_id);
    }
    add_action('personal_options_update', 'update_extra_profile_fields');
    </form>
    

    this must work so that you can update the fields in the user dahsboard from the custom form.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘How update custom field checkbox ACF in user profile from front end’ is closed to new replies.