Help with the Repeater (beta) Code

I am trying to use the Repeater (beta) code to create a group of fields that can be brought up by clicking a button, but when I bring up the page, I am seeing nothing. Here is the code I am using in an HTML content field within the form:

And now here is a screenshot of the form’s page where it should be popping up:

What am i doing wrong? Any help will be greatly appreciated!

Here is the code:

// Adjust your form ID
add_filter( 'gform_form_post_get_meta_149', 'add_my_field' );
function add_my_field( $form ) {
 
    // Create a Section header for Family Household Member
    $FamilyMember = GF_Fields::create( array(
        'type'   => 'section',
        'id'     => 1001, // The Field ID must be unique on the form
        'formId' => $form['2'],
        'label'  => 'Household Member',
        'pageNumber'  => 2, // Ensure this is correct
    ) );
 
    // Create a Single Line text field for the family member's first name
    $FamilyFirstName = GF_Fields::create( array(
        'type'   => 'text',
        'id'     => 1002, // The Field ID must be unique on the form
        'formId' => $form['2'],
        'label'  => 'First Name',
        'pageNumber'  => 2, // Ensure this is correct
    ) );
 
    // Create a Single Line text field for the family member's middle name
    $FamiliyMiddleName = GF_Fields::create( array(
        'type'   => 'text',
        'id'     => 1003, // The Field ID must be unique on the form
        'formId' => $form['2'],
        'label'  => 'Middle Name (optional)',
        'pageNumber'  =>; 2, // Ensure this is correct
    ) );
 
    // Create a Single Line text field for the family member's last name
    $FamilyLastName = GF_Fields::create( array(
        'type'   => 'text',
        'id'     => 1004, // The Field ID must be unique on the form
        'formId' => $form['2'],
        'label'  => 'Last Name',
        'pageNumber'  => 2, // Ensure this is correct
    ) );
 
    // Create a Single Line text field for the family member's relationship to you
    $FamilyRelationship = GF_Fields::create( array(
        'type'   => 'text',
        'id'     => 1005, // The Field ID must be unique on the form
        'formId' => $form['2'],
        'label'  => 'Relationship to You',
        'pageNumber'  => 2, // Ensure this is correct
    ) );
 
    // Create an date field for the family member's date of birth
    $FamilyDateOfBirth = GF_Fields::create( array(
        'type'   => 'date',
        'id'     => 1006, // The Field ID must be unique on the form
        'formId' => $form['2'],
        'label'  => 'Date of Birth',
        'pageNumber'  => 2, // Ensure this is correct
    ) );
 
    // Create a repeater for the team members and add the name and email fields as the fields to display inside the repeater.
    $team = GF_Fields::create( array(
        'type'             => 'repeater',
        'description'      => 'Maximum of 9 family members  - set by the maxItems property',
        'id'               => 1000, // The Field ID must be unique on the form
        'formId'           => $form['2'],
        'label'            => 'Family Members',
        'addButtonText'    => 'Add team member', // Optional
        'removeButtonText' => 'Remove team member', // Optional
        'maxItems'         => 9, // Optional
        'pageNumber'       => 2, // Ensure this is correct
        'fields'           => array( $FamilyFirstName, $FamilyMiddleName, $FamilyLastName, $FamilyRelationship, $FamilyDateOfBirth ), // Add the fields here.
    ) );
 
    $form['fields'][] = $family;
 
    return $form;
}
 
// Remove the field before the form is saved. Adjust your form ID
add_filter( 'gform_form_update_meta_149', 'remove_my_field', 10, 3 );
function remove_my_field( $form_meta, $form_id, $meta_name ) {
 
    if ( $meta_name == 'display_meta' ) {
        // Remove the Repeater field: ID 1000
        $form_meta['fields'] = wp_list_filter( $form_meta['fields'], array( 'id' => 1000 ), 'NOT' );
    }
 
    return $form_meta;}

This line limits the code so it only runs for form ID 149. You’ll need to change that to 2 e.g.

add_filter( 'gform_form_post_get_meta_2', 'add_my_field' );

You’ll also need to make the same change to the other filter e.g.

add_filter( 'gform_form_update_meta_2', 'remove_my_field', 10, 3 );

Another issue with the code is this line:

'formId' => $form['2'],

The form object doesn’t have a property named 2 unless you have added a custom property so to get the form id that line should be this:

'formId' => $form['id'],
2 Likes

Thank you @richardw8k. I have made those changes, but nothing is coming up yet. Does it matter where the code is placed? I currently have it within the second page.

@richardw8k I found out that putting the code at the first of the form does nothing, so what else could it be? What all else is included or described in the filter lines concerning the form?

Hi Nathan. That is PHP code which should go into your theme functions.php file or a custom functionality plugin:

It does not go into an HTML field in the form.

1 Like

Thank you @chrishajer. I understand how to add the code into the functions.php, but my themes folder doesn’t have a functions.php file. You mentioned about a custom functionality plugin. Which plugin would you suggest or what else should/could I do?

You can use this to add a custom functionality plugin to your site. It’s a better idea than adding the code into the theme functions.php file:

1 Like

Very easy to use! The functionality is still not coming up though:

Is there something else I am missing to do. Is there some sort of call I need to make on the form to be able to bring it all up?

Can you post your updated code please, and the URL to a page on your site where we can see the embedded form? Thank you.

Of course.

Here is the code:

// Adjust your form ID
add_filter( 'gform_form_post_get_meta_2', 'add_my_field' );
function add_my_field( $form )
{
    // Create a Section header for Family Household Member
    $FamilyMember = GF_Fields::create( array(
        'type'   => 'section',
        'id'     => 1001, // The Field ID must be unique on the form
        'formId' => $form['id'],
        'label'  => 'Household Member',
        'pageNumber'  => 2, // Ensure this is correct
    ) );
 
    // Create a Single Line text field for the family member's first name
    $FamilyFirstName = GF_Fields::create( array(
        'type'   => 'text',
        'id'     => 1002, // The Field ID must be unique on the form
        'formId' => $form['id'],
        'label'  => 'First Name',
        'pageNumber'  => 2, // Ensure this is correct
    ) );
 
    // Create a Single Line text field for the family member's middle name
    $FamiliyMiddleName = GF_Fields::create( array(
        'type'   => 'text',
        'id'     => 1003, // The Field ID must be unique on the form
        'formId' => $form['id'],
        'label'  => 'Middle Name (optional)',
        'pageNumber'  =>; 2, // Ensure this is correct
    ) );
 
    // Create a Single Line text field for the family member's last name
    $FamilyLastName = GF_Fields::create( array(
        'type'   => 'text',
        'id'     => 1004, // The Field ID must be unique on the form
        'formId' => $form['id'],
        'label'  => 'Last Name',
        'pageNumber'  => 2, // Ensure this is correct
    ) );
 
    // Create a Single Line text field for the family member's relationship to you
    $FamilyRelationship = GF_Fields::create( array(
        'type'   => 'text',
        'id'     => 1005, // The Field ID must be unique on the form
        'formId' => $form['id'],
        'label'  => 'Relationship to You',
        'pageNumber'  => 2, // Ensure this is correct
    ) );
 
    // Create a Date field for the family member's date of birth
    $FamilyDateOfBirth = GF_Fields::create( array(
        'type'   => 'date',
        'dateType'  => 'datefield',
        'dateFormat'  => 'dd/mm/yyyy',
        'id'     => 1006, // The Field ID must be unique on the form
        'formId' => $form['id'],
        'label'  => 'Date of Birth',
        'pageNumber'  => 2, // Ensure this is correct
    ) );
 
    // Create a repeater for the team members and add the name and email fields as the fields to display inside the repeater.
    $family = GF_Fields::create( array(
        'type'             => 'repeater',
        'description'      => 'Maximum of 9 family members  - set by the maxItems property',
        'id'               => 1000, // The Field ID must be unique on the form
        'formId'           => $form['id'],
        'label'            => 'Family Members',
        'addButtonText'    => 'Add team member', // Optional
        'removeButtonText' => 'Remove team member', // Optional
        'maxItems'         => 9, // Optional
        'pageNumber'       => 2, // Ensure this is correct
        'fields'           => array( $FamilyFirstName, $FamilyMiddleName, $FamilyLastName, $FamilyRelationship, $FamilyDateOfBirth ), // Add the fields here.
    ) );
 
    $form['fields'][] = $family;
 
    return $form;
}
 
// Remove the field before the form is saved. Adjust your form ID
add_filter( 'gform_form_update_meta_2', 'remove_my_field', 10, 3 );
function remove_my_field( $form_meta, $form_id, $meta_name ) {
 
    if ( $meta_name == 'display_meta' ) {
        // Remove the Repeater field: ID 1000
        $form_meta['fields'] = wp_list_filter( $form_meta['fields'], array( 'id' => 1000 ), 'NOT' );
    }
 
    return $form_meta;}

The multi-page form can be started at https://servefoundationcom.wpcomstaging.com/apply/. The part where the code is supposed to working is at https://servefoundationcom.wpcomstaging.com/apply/#gf_2

@chrishajer just checking, have you had a chance to review the code? At one point it created an error on the site and I had to turn it off. I haven’t tried it with what I have posted on here just yet, I wanted to read your review first.

Hi Nathan. This line has an extra semi-colon before the “2” (you should remove that):

 'pageNumber'  =>; 2, // Ensure this is correct

I don’t see any other syntax errors, so that may have been causing the error message you saw. Can you use the code again, then share the link to the page on the site where we can see the form again? Thank you.

Thank you @chrishajer I have fixed that and turned on the code, but when I go back to the page (which is at https://servefoundationcom.wpcomstaging.com/apply/#gf_2), nothing comes up with the code. When I go back to the plugin page it says that the code is off now. It isn’t giving me any errors, but neither does it seem to be running. Have you ever had that happen?

@chrishajer, I figured out what I did wrong. I had forgotten click save after starting it. Now the form page looks like it’s completely broken though. You can see it at https://servefoundationcom.wpcomstaging.com/apply/

@chrishajer it looks like all of the header images broke throughout the site after I activated the code. Deactivating the code doesn’t bring it back either. Could I have something in there that would mess with those images?

Hi Nathan. I went to view the site and the first time I looked I had just a colored bar up top and some text. I refreshed the page to see if I could load anything, and I saw this:

What is the error message that was sent to the website administrator? That may help pinpoint the issue.

@chrishajer I have only ever seen just the colored bar, but that could be because I’m using Chrome instead of Firefox.

I haven’t seen any error message either. Let me ask another person in the company to see if they received one.

The email address listed as website administrator on the settings page will receive the email.

Thank you Chris. I will check with that person.