bookmark_bordergform_countries

Description

This filter can be used to add or remove countries from the address field country drop down.

Usage

add_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)

        array( '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.

add_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.

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

    sort( $countries );

    return $countries;
} );

3. Use Country Code as Value

This example shows how you can update the countries list to use the country code as the choice value.

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;
} );

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