Adding/Removing Countries From the Country Select Field

In some instances, you may need to add or remove a particular country to or from the country selection field in Gravity Forms. Here’s how to do it.

Doing so will require using a filter within your code. This code would be placed within your theme’s functions.php file, or ideally, within its own plugin.

Removing a Country

add_filter( 'gform_countries', 'remove_country' );
function remove_country( $countries ) {
    $key = array_search( 'United States', $countries );
    unset( $countries[ $key ] );
    return $countries;
}

As you can see from the above snippet, the gform_countries filter is being called and replacing the default information with the return value of the remove_country function, after finding and removing the United States option.

Adding a Country

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

In this example, the gform_countries filter is being called and adding an additional country to the array. From here, we then sort the listing and return the $countries array.