Support

Account

Home Forums ACF PRO Repeaterfield in ACF User Registration Form

Solved

Repeaterfield in ACF User Registration Form

  • Hi,

    I created a Frontend User Registration Form that contains ACF-fields (ACF PRO 5). With all kinds of fields this works just fine, but I can’t get a repeater field to work.
    I created a repeater-field with 3 sub-fields and they do show on the registration page (frontend) but I can’t get those values in the database.

    I’m using ‘acf/save_post’ to create a dummy post, create a new user, update all user_meta (by using ‘update_user_meta’ to get all values into the right fields.

    I use this code to get the data from the form into the user_meta:

    
    <?php
    // USER REGISTRATION - CUSTOM GROUP FOR USER Registration
    function my_acf_save_user( $post_id ) {
    
      if( wp_is_post_revision( $post_id ) || is_admin() ) {
        return;
      }
    
      // get fields
      $sex = get_field('geslacht', $post_id);
      $initials = get_field('voorletters', $post_id);
      $first_name = get_field('voornaam', $post_id);
      $insertion = get_field('tussenvoegsel', $post_id);
      $last_name = get_field('achternaam', $post_id);
      $email = get_field('email', $post_id);
      $linkedin = get_field('linkedin', $post_id);
      $employer = get_field('naam_werkgever', $post_id);
      $postalcode = get_field('postcode_werkgever', $post_id);
      $houseNumber = get_field('huisnummer_werkgever', $post_id);
      $numberAddition = get_field('toevoeging_huisnummer_werkgever', $post_id);
      $street = get_field('straatnaam_werkgever', $post_id);
      $place = get_field('woonplaats_werkgever', $post_id);
      $province = get_field('provincie_werkgever', $post_id);
    
      $first_name2 = str_replace(' ', '-', strtolower($first_name));
      $insertion2 = str_replace(' ', '-', strtolower($insertion));
      $last_name2 = str_replace(' ', '-', strtolower($last_name));
    
      if( $insertion != '' ) {
        // create the user
        $user_id = wp_create_user( $first_name2 . "_" . $insertion2 . "_" . $last_name2, bin2hex(random_bytes(16)), $email );
      } else {
        // create the user
        $user_id = wp_create_user( $first_name2 . "_" . $last_name2, bin2hex(random_bytes(16)), $email );
      }
    
      update_user_meta( $user_id, 'geslacht', $sex );
      update_user_meta( $user_id, 'voorletters', $initials );
      update_user_meta( $user_id, 'voornaam', $first_name );
      update_user_meta( $user_id, 'tussenvoegsel', $insertion );
      update_user_meta( $user_id, 'achternaam', $last_name );
      update_user_meta( $user_id, 'email', $email );
      update_user_meta( $user_id, 'linkedin', $linkedin );
      update_user_meta( $user_id, 'naam_werkgever', $employer );
      update_user_meta( $user_id, 'postcode_werkgever', $postalcode );
      update_user_meta( $user_id, 'huisnummer_werkgever', $houseNumber );
      update_user_meta( $user_id, 'toevoeging_huisnummer_werkgever', $numberAddition );
      update_user_meta( $user_id, 'straatnaam_werkgever', $street );
      update_user_meta( $user_id, 'woonplaats_werkgever', $place );
      update_user_meta( $user_id, 'provincie_werkgever', $province );
    
      // delete the unused post
      wp_delete_post($post_id);
    
    }
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_user', 20);
    

    Can someone give me a help how to get the a repeater-field with 3 subfield and their values into the user_meta.
    Any help would be appreciated.

    Thanks in advance

    Kind regards,
    Job

  • What are the fields in the above code that are from the repeater?

    Past that, your code can run into problems. You should be using update_field() https://www.advancedcustomfields.com/resources/update_field/ and you should be using the field keys and not the field names as explained on this page. How to construct the correct $post_id value for a user is explained on this page https://www.advancedcustomfields.com/resources/get_field/

    With that said, inserting a repeater requires constructing an array of values

    
    // array holding all of the rows of the repeater
    $values = array(
      // a nested array for each row
      array(
        // field key => value pairs for this row
        'field_123456' => 'value of field',
      ),
    );
    // update field using field key
    update_field('field_xyz543', $values, 'user_'.$user_id');
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Repeaterfield in ACF User Registration Form’ is closed to new replies.