DescriptionUsageParametersExamples1. Limit to Specific Countries2. Add New Countries3. Use Country Code as ValuePlacementSource Code
Description
This filter can be used to add or remove countries from the address field Country drop down.
Usage
1add_filter( 'gform_countries', 'your_function_name' );
Parameters
$countries array
The array to be filtered. It contains The list of countries in a standard array (see sample below).
1array( 'Argentina', 'Brazil', 'Netherlands', 'United States', 'United Kingdom', ... );
Examples
1. Limit to Specific Countries
This example demonstrates how to limit the country drop down to only a few select countries.
1234add_filter( 'gform_countries', 'remove_country' );function remove_country( $countries ){ return array( 'Brazil', 'United States', 'Netherlands', 'United Kingdom' );}
2. Add New Countries
This example demonstrates how to add new countries to the list.
12345678add_filter( 'gform_countries', function ( $countries ) { $countries[] = 'Country 1'; $countries[] = 'Country 2'; sort( $countries ); return $countries;} );
3. Use Country Code as Value
These examples show how you can update the countries list to use the country code as the choice value.
The following example requires Gravity Forms 2.4.19.3+.
123456add_filter( 'gform_countries', function () { $countries = GF_Fields::get( 'address' )->get_default_countries(); asort( $countries ); return $countries;} );
The following example works with Gravity Forms 1.9+.
12345678910add_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;} );
Placement
This code should be placed in the functions.php file of your active theme.
Source Code
This filter is located in GF_Field_Address::get_countries() in includes/fields/class-gf-field-address.php.