Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active March 22, 2020 06:38
Show Gist options
  • Save Shelob9/fa9db37404b9c0c727c7455854321316 to your computer and use it in GitHub Desktop.
Save Shelob9/fa9db37404b9c0c727c7455854321316 to your computer and use it in GitHub Desktop.
Change Caldera Forms phone field default country. See: https://calderaforms.com/doc/caldera_forms_phone_js_options/
<?php
/**
* Set preffered countries for Caldera Forms phone fields
*/
add_filter( 'caldera_forms_phone_js_options', function( $options){
//Use ISO_3166-1_alpha-2 formatted country code
$options[ 'preferredCountries' ] = array( 'MX' );
return $options;
});
<?php
/**
* Set intiial country for Caldera Forms phone fields
*/
add_filter( 'caldera_forms_phone_js_options', function( $options){
//Use ISO_3166-1_alpha-2 formatted country code
$options[ 'initialCountry' ] = 'CH';
return $options;
});
<?php
/**
* Show only specific countries' flag in Caldera Forms phone fields
*/
add_filter( 'caldera_forms_phone_js_options', function($options ){
$options[ 'onlyCountries' ] = array( 'cn', 'tw' );
return $options;
});
@Ascar699
Copy link

@rendermanGL,

I am wondering if you have a readily available solution or something in the making for functionality whereas GeoIP data is pulled before the form loads and then the matching country flag is fetched based on the IP address.

Alternatively and equally cool way to handle this would be based on user input - Country field should be on the form before the phone number and once a user selects the country the form would update the country code/flag in the phone field accordingly?

Thanks

@Nkistoe
Copy link

Nkistoe commented Oct 24, 2019

How do I do this in the wordpress astra theme? thank you

@Nasirkhan-259
Copy link

I have the same issue i am using astra theme for some reason when i put the above code it works for the first time but when i reload again , the default country is gone again ,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment