Using Gravity Forms ‘gform_countries’ PHP filter

The gform_countries Gravity Forms PHP filter allows you to modify the list of countries displayed in the address field Country drop down.

Usage

add_filter('gform_countries', 'your_function_name');

Parameters

  • $countries (array): The array containing the list of countries, e.g.,
    array('Argentina', 'Brazil', 'Netherlands', 'United States', 'United Kingdom', ...);

More information

See Gravity Forms Docs: gform_countries

Examples

Limit to Specific Countries

This code limits the country drop down to only a few select countries.

add_filter('gform_countries', 'remove_country');

function remove_country($countries){
    return array('Brazil', 'United States', 'Netherlands', 'United Kingdom');
}

Add New Countries

This code adds new countries to the list.

add_filter('gform_countries', function ($countries) {
    $countries[] = 'Country 1';
    $countries[] = 'Country 2';
    sort($countries);
    return $countries;
});

Use Country Code as Value

This code updates the countries list to use the country code as the choice value. The first example requires Gravity Forms 2.4.19.3+.

add_filter('gform_countries', function () {
    $countries = GF_Fields::get('address')->get_default_countries();
    asort($countries);
    return $countries;
});

The second example works with Gravity Forms 1.9+.

add_filter('gform_countries', function ($countries) {
    $new_countries = array();
    foreach ($countries as $country) {
        $code = GF_Fields::get('address')->get_country_code($country);
        $new_countries[$code] = $country;
    }
    return $new_countries;
});

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