Using Gravity Forms ‘gform_default_address_type’ PHP filter

The gform_default_address_type is a Gravity Forms PHP filter that allows you to override the default address type for new fields. This filter can be used together with the gform_address_types filter to add support for new country-specific address types.

Usage

A generic example of how to use the filter:

add_filter('gform_default_address_type', 'your_function_name', 10, 2);

To target a specific form, append the form ID to the hook name:

add_filter('gform_default_address_type_5', 'your_function_name', 10, 2);

Parameters

  • $default_address_type (string): The default address type, international.
  • $form_id (integer): The current form ID.

More information

See Gravity Forms Docs: gform_default_address_type

Examples

Set default address type to Australia

This example sets the default address type to Australia. The Australian address type must have been added by the gform_address_types filter.

add_filter('gform_default_address_type', function ($default_address_type, $form_id) {
    return 'australia';
}, 10, 2);

Note: Place this code in the functions.php file of your active theme.

Set default address type to US for a specific form

This example sets the default address type to US for a form with an ID of 7.

add_filter('gform_default_address_type_7', function ($default_address_type, $form_id) {
    return 'us';
}, 10, 2);

Set default address type based on user location

This example sets the default address type based on the user’s country detected from their IP address.

function set_address_type_based_on_user_location($default_address_type, $form_id) {
    $user_country = get_user_country_from_ip(); // Replace with a function that retrieves user's country from IP address
    return $user_country == 'US' ? 'us' : 'international';
}
add_filter('gform_default_address_type', 'set_address_type_based_on_user_location', 10, 2);

Set default address type for multiple forms

This example sets the default address type to Canada for forms with IDs 3 and 5.

function set_default_address_type_for_multiple_forms($default_address_type, $form_id) {
    if (in_array($form_id, array(3, 5))) {
        return 'canada';
    }
    return $default_address_type;
}
add_filter('gform_default_address_type', 'set_default_address_type_for_multiple_forms', 10, 2);

Set default address type based on form category

This example sets the default address type to UK for forms with the category “UK Forms”.

function set_default_address_type_based_on_category($default_address_type, $form_id) {
    $form_categories = get_the_terms($form_id, 'gravity_form_category'); // Replace with the correct taxonomy for your forms
    foreach ($form_categories as $category) {
        if ($category->name == 'UK Forms') {
            return 'uk';
        }
    }
    return $default_address_type;
}
add_filter('gform_default_address_type', 'set_default_address_type_based_on_category', 10, 2);

Note: This example assumes that you have a custom taxonomy called “gravity_form_category” .