GF_Fields and pageNumber

Hi there,

I’m trying to programatically create fields using the GF_Fields::create functions and that includes the pageNumber parameter. When returing the field using $form[‘fields’], the fields get added to the very end of the page. I tried using array_unshift($form[‘fields’], $custom_field), but this adds the field to the very beginning.

So in both cases the pageNumber parameter is being ignored. How can I add the field so that the pageNumber is correctly applied?

Regards

The pageNumber property does not determine which page the field is displayed on. That property is used by the field validation feature to determine which page failed validation when the field fails validation.

Here’s the approach we recommend for adding a field to the form:

array_splice( $form['fields'], 2, 0, array( $custom_field ) );

The fields array starts with an index of 0. The above line is adding the custom field at index 2, making it the third field on the form. You will need to determine what number you need to change the 2 in the example above to so that the custom field falls between the range of fields in your form where you would like it to be located.

1 Like

Hi @richardw8k , thank for the quick response. I have two fields that are added (let’s say field ‘car’ and field ‘motorbike’). The code for the motorbike field is added later in the functions.php, but is rendered before the car field (even though it’s basically using the same code). I’m not sure, how many fields my form will have, but these two fields have to be at the end and car has to go before motorbike field.

My question is - can i use the array_splice function you provided with just really high numbers? For motorbike eg.

array_splice( $form['fields'], 1000, 0, array( $custom_field ) );

and for car eg:

array_splice( $form['fields'], 2000, 0, array( $custom_field ) );

The number you use must be a valid key to the $form['fields'] array. If your form has 2000+ fields, then those numbers would insert the fields as the 999 and 1999th fields on the form. But if your form doesn’t have that many fields, those numbers won’t work.

Okay, but what is the solution, if I do not know the final number of fields in an entry? We are using numerous forms with different number of fields.

To be more specific, the function I’m using to add fields programatically is using a WP query to add several fields from two custom post type (let’s say 10 fields from CTP ‘consent’ and 10 fields from CTP ‘info’). I need both groups of fields shown at the very end of the form, but the ‘info’ group of fields needs to be shown after the ‘consent’ group of fields.

I tried changing the order of the code in functions.php, but that did not help. I can’t figure out, how to change the order of these two groups of programatically added fields (each group has it’s own functions with WP query that adds the respective fields into the form).

One idea that just crossed my mind - is it somehow possible to get the highest field ID from the $form object? I could use this as a variable in your function:

array_splice( $form['fields'], $highest_id, 0, array( $custom_field ) );