Get field by custom name

I have a WordPress multisite setup that uses the same multi-step form. The form id and field id’s are different for every subsite. In every step of the form, I need to read the data from the previous step, and send it to a third party service. The $_POST variable has a bunch of fields, but they all use id’s for the field names (i.e. input_148).

I want to change these names so they make more sense to me. I.e. input_148 should be total_monthly_costs. If I need to target them by id’s, I have a bunch of magic numbers in my code which is not maintainable.

I tried using Advanced > Allow field to be populated dynamically which allows me to name the field, but this doesn’t work as I expected (it seems to populate the field based on a query parameter).

If renaming the name of a field is not possible, I’d like to know of some other way in which I can get the value of an input field without using numbered id’s. Thanks!

Hi Gerben. If you log the $_POST in your code, you will see what is available. Here’s an example from one of my forms:

(
    [input_79c364343a3ecd91f73e3c13f4608320] => 03AEkXODDTNpn8pzuMoeKaBiKEP0qzxiZCxWpNXRnQkyO_KuHaKiE-TxMre_i5-AOyP_BY03es_6SYBixfwynwvFm_eXWCawRRvJ4nILR5S6tqz16CRwSqiMbOi19_cU-r0pM_OwW2N0DH8Fsh69bsUZ6m6NMyxag7banZjLShsPicFJFs1IaxR4ya0TqORjVYqeAeSg1w8L_4O636uJHxTMEvTzIB_M_1jevHi4-Op7wWnCa2ZEDn-HMi6HjgRymfkSOuEz_hsRzK72GMU2qR6DnbI8fYsWp8fmY4_g3FRwue20xJAEa1rFUWT4TfOO3GfNah79tG96i1t_n1FmkFRvpFCvRY5uu2TCl26Pa-D6KOQreEayjWoCcYVhkQisQwlx_NUAPOQf1hRAiQq6s10XFe9BvXS2fCxpyOZlpYepwirlVrBfkOHv0wZsDR79okGukNDanCGZltLWvAX8H0zo0ZvyftdZnG0s1IAroBS7NSmQlT7q46HpexkGYTAehO0kW6gjcw3JDfsg1J392tY1DwrPM9C0tffA
    [input_4] => Veda
    [input_6] => Bartlett
    [input_33] => Hines Blake Trading
    [input_10_2] => WEBSITE
    [input_10_3] => BUSINESS CARDS
    [input_10_8] => ILLUSTRATION
    [input_10_9] => EMAIL SIGNATURE
    [input_10_11] => SOCIAL MEDIA
    [input_10_15] => LETTERHEAD
    [input_10_16] => VEHICLE GRAPHICS
    [input_10_17] => HOSTING
    [input_10_18] => SEO
    [input_10_21] => BRAND NAMING
    [input_10_22] => SIGNAGE
    [input_10_23] => OTHER
    [input_11] =>
    [input_14] => Large e-commerce, many products, complex functionality
    [input_29] => 604
    [input_30] => 102
    [input_36] => 3 - 6 months
    [input_39] => Eveniet ipsum natus
    [input_42] => Building signage
    [input_22] => fadysybov@mailinator.com
    [input_23] => +1 (372) 366-1231
    [input_24_2] => Leave this button selected to join our mailing list. We send emails once a month with exclusive offers, tips, and our latest work.
    [input_24_3] => 73
    [is_submit_768] => 1
    [gform_submit] => 768
    [gform_unique_id] => 8f4b554e
    [state_768] => WyJ7XCIyNC4xXCI6XCJiNDBjMDhlNjgzNWE0Y2MwNWJkYzQ4ZTlhYzk0ZGRlM1wiLFwiMjQuMlwiOlwiZTJmNzU0ZDQwYzI2ODA3NTU2YmEwZGI0YWEyYzFhNzhcIixcIjI0LjNcIjpcIjg0ZTFmMTk0ZDQxMzY4MGVhMjhhYTNkMGFlMWUxNjdkXCJ9IiwiMzlhZDJjNDk1NDRlMmM4MTI2NTNiMmM5OTAxNjljZWQiXQ==
    [gform_target_page_number_768] => 0
    [gform_source_page_number_768] => 1
    [gform_field_values] =>
    [gform_uploaded_files] => {\"input_16\":[{\"temp_filename\":\"8f4b554e_input_16_exAG9onGKC4e3gKF_o_1gidoom9rh181v6e1nj914aa10ahk.png\",\"uploaded_filename\":\"1150-April-6.png\"},{\"temp_filename\":\"8f4b554e_input_16_kY5C4Ulr2lLqiqpb_o_1gidoo9f877h14v714i71qio1e9jf.pdf\",\"uploaded_filename\":\"E-Learning-Days_-District-Expectations-for-Students.pdf\"}]}
)

You can see that only the input IDs are available. But the form ID is available in [state_768]. With that, you could get the $form object, like this:

$result = GFAPI::get_form( $form_id );

Documentation:

You could then loop through the $form object and see what it contains. Here’s an example for a form object. This is for form 768 also:

Now, with the form ID and the form object, you could do something like:

foreach ( $form['fields'] as &$field ) {
	// check each field for the existence of the cssClass "custom__interest"
	if ( strpos( $field->cssClass, 'custom__interest' ) === false ) {
		continue; // i.e. skip the field
	}
	else {
		// do something;
	}
}

I gave a field a custom CSS Class name of custom__interest. This code example will loop through the form object looking for that custom CSS Class name and then perform code on that field. I’m not sure if that is easier than the field IDs for you, but hopefully this approach will help you.

Hi @chrishajer, thanks for you reply. I was hoping there would be a more performant way to find a field, but performance probably is not really an issue. I’ll give it a try. Thank you again.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.