Adding/Removing Countries From the Country Select Field

Adding/Removing Countries From the Country Select Field

Removing a CountryAdding a Country

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 it』s 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.

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 replacing the default information with the add_country function, which is adding an additional country to the array. From here, we then sort the listing, and return the $countries array.

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注