How to Add Country Specific Address Fields to Gravity Forms in WordPress

Home » Snippets » How to Add Country Specific Address Fields to Gravity Forms in WordPress
0

Created with:

Visibility: 

public

Creator: Alex

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

If you own a website that requires users to fill out forms with their address information, you may be looking for a way to make the process more user-friendly. One way to do this is by adding country-specific address fields to your forms. With Gravity Forms, you can easily customize your forms to include specific fields based on the user’s country selection. In this article, we’ll show you how to do just that.

					function wpturbo_custom_gf_country_fields( $fields ) {

    // Define an array of country codes and their corresponding address fields
    $countries_fields = array(
        'US' => array(
            'street_address_2',
            'state',
            'zip'
        ),
        'CA' => array(
            'street_address_2',
            'province',
            'postal_code'
        ),
        // Add more countries and fields as needed
    );

    // Get the selected country from the form
    $selected_country = rgpost( 'input_3' );

    // If the selected country has corresponding address fields, add them to the form
    if ( array_key_exists( $selected_country, $countries_fields ) ) {
        foreach ( $countries_fields[ $selected_country ] as $field ) {
            $fields[] = array(
                'id'        => 6,
                'label'     => __( 'Address', 'wpturbo' ) . ' ' . $field,
                'type'      => 'text',
                'required'  => true
            );
        }
    }

    return $fields;
}
add_filter( 'gform_fields', 'wpturbo_custom_gf_country_fields' );
				

The code snippet above shows how to add country-specific address fields to a Gravity Forms form. This can be useful if you want to collect additional information from users based on their country of origin.

The code starts by defining an array called $countries_fields. This array holds country codes as keys and arrays of corresponding address fields as values. In the example, there are only two countries defined, the US and Canada, but you can add as many countries as needed to this array.

Next, the code uses the rgpost() function to get the selected country from the form. The country field ID is hardcoded in the function call as input_3. You should replace this with the actual ID of your country field.

Then, the code checks if the selected country has corresponding address fields defined in the $countries_fields array. If it does, it loops through them and adds a new field to the $fields array for each one.

Each added field has an ID, label, type, and required attribute. In this case, the field type is set to ‘text’, but you can change this to any other supported field type, such as ‘dropdown’, ‘radio’, or ‘checkboxes’, depending on the type of input you want to collect.

Finally, the code returns the modified $fields array and hooks the wpturbo_custom_gf_country_fields() function into the gform_fields filter to apply the changes.

To use this code, you should copy and paste it into your theme’s functions.php file, or a custom plugin. Be sure to modify the $countries_fields array and the ID of the country field to match your form’s structure.

Register an account to save your snippets or go Pro to get more features.