Title: Country Code or Dial Code getting failed to load
Last modified: January 16, 2023

---

# Country Code or Dial Code getting failed to load

 *  Resolved [prabhjas](https://wordpress.org/support/users/prabhjas/)
 * (@prabhjas)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/country-code-or-dial-code-getting-failed-to-load/)
 * Dear Brother,
   I see again new API is giving an issue. Sometimes country selection
   is coming and sometimes it’s not.Could you please help?Below is the error:Uncaught
   TypeError: Cannot read properties of null (reading ‘country_code2’)I’ll be waiting
   for your prompt response.

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/country-code-or-dial-code-getting-failed-to-load/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/country-code-or-dial-code-getting-failed-to-load/page/2/?output_format=md)

 *  [kanhaiya122108](https://wordpress.org/support/users/kanhaiya122108/)
 * (@kanhaiya122108)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/country-code-or-dial-code-getting-failed-to-load/#post-16379841)
 * [@prabhjas](https://wordpress.org/support/users/prabhjas/) , Im facing same issue
   and i found that the api they used are showing connection timeout error. Error:**
   Web server is returning an unknown error Error code 520**
    -  This reply was modified 3 years, 4 months ago by [kanhaiya122108](https://wordpress.org/support/users/kanhaiya122108/).
 *  Thread Starter [prabhjas](https://wordpress.org/support/users/prabhjas/)
 * (@prabhjas)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/country-code-or-dial-code-getting-failed-to-load/#post-16379950)
 * Hi [@kanhaiya122108](https://wordpress.org/support/users/kanhaiya122108/) 
   Yup.
   Let me know if you find any solution for the same.[@narinder](https://wordpress.org/support/users/narinder/)
   brother please check and help.Thanks.
 *  [cinikdevteam](https://wordpress.org/support/users/cinikdevteam/)
 * (@cinikdevteam)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/country-code-or-dial-code-getting-failed-to-load/#post-16380056)
 * Same issue here
 *  [subhansafdars](https://wordpress.org/support/users/subhansafdars/)
 * (@subhansafdars)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/country-code-or-dial-code-getting-failed-to-load/#post-16380252)
 * I’m also facing same issue.
   Uncaught TypeError: Cannot read properties of null(
   reading ‘country_code2’)Please check this ASAP.Thnaks
 *  [Rob Skinn](https://wordpress.org/support/users/robskinn/)
 * (@robskinn)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/country-code-or-dial-code-getting-failed-to-load/#post-16384744)
 * Same here.
 *  [meshackcris](https://wordpress.org/support/users/meshackcris/)
 * (@meshackcris)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/country-code-or-dial-code-getting-failed-to-load/#post-16389510)
 * i actually fixed this myself, but i don’t think it’s right to share it here. 
   since i do not own this plugin
 *  Thread Starter [prabhjas](https://wordpress.org/support/users/prabhjas/)
 * (@prabhjas)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/country-code-or-dial-code-getting-failed-to-load/#post-16389529)
 * [@meshackcris](https://wordpress.org/support/users/meshackcris/) happy to hear
   that. It would be great if you can help others too. We’re not going to mark this
   plugin on our name. It will remain with author only.
 *  [subhansafdars](https://wordpress.org/support/users/subhansafdars/)
 * (@subhansafdars)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/country-code-or-dial-code-getting-failed-to-load/#post-16390187)
 * [@meshackcris](https://wordpress.org/support/users/meshackcris/) please share
   solution.
   It will be great help for lot of prople. Thanks
 *  [meshackcris](https://wordpress.org/support/users/meshackcris/)
 * (@meshackcris)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/country-code-or-dial-code-getting-failed-to-load/#post-16392292)
 * Shoot me an email, if you need my fix until the developers respond.
 * Note: the code will be overwritten when developers pushed a new update(probably
   issue would’ve been fixed then)
 * [meshackcycris@gmail.com](https://wordpress.org/support/topic/country-code-or-dial-code-getting-failed-to-load/meshackcycris@gmail.com?output_format=md)
 *  [meshackcris](https://wordpress.org/support/users/meshackcris/)
 * (@meshackcris)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/country-code-or-dial-code-getting-failed-to-load/#post-16392307)
 * i am not sure i could paste the code here (300 lines)
 *  [meshackcris](https://wordpress.org/support/users/meshackcris/)
 * (@meshackcris)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/country-code-or-dial-code-getting-failed-to-load/#post-16392313)
 *     ```wp-block-code
       <?php
   
       /* Include all js and css files for active theme */
       function nb_cpf_embedCssJs() {
   
           wp_enqueue_style( 'nbcpf-intlTelInput-style', NB_CPF_URL . 'assets/css/intlTelInput.min.css' );
       	wp_enqueue_style( 'nbcpf-countryFlag-style', NB_CPF_URL . 'assets/css/countrySelect.min.css' );
       	wp_enqueue_script( 'nbcpf-intlTelInput-script', NB_CPF_URL . 'assets/js/intlTelInput.min.js', array( 'jquery' ), false, true );
       	wp_enqueue_script( 'nbcpf-countryFlag-script', NB_CPF_URL . 'assets/js/countrySelect.min.js', array( 'jquery' ), false, true );
   
       	wp_localize_script( 'nbcpf-countryFlag-script', 'nbcpf', array(
       		'ajaxurl' => site_url() . '/wp-admin/admin-ajax.php', // WordPress AJAX
       	) );
   
       	$nb_cpf_settings_options = get_option( 'nb_cpf_options' );
       	$IPaddress  =   $_SERVER['REMOTE_ADDR'];
   
       	if(isset( $nb_cpf_settings_options['defaultCountry'] ) && $nb_cpf_settings_options['defaultCountry'] !=''){
       		$defaultCountry = 'defaultCountry: "'.strtolower( $nb_cpf_settings_options['defaultCountry'] ).'",';
   
       	} else {
       		$defaultCountry = '';
   
       	}
       	if(isset( $nb_cpf_settings_options['onlyCountries'] ) && $nb_cpf_settings_options['onlyCountries'] !=''){
       		$onlyCountries = 'onlyCountries: '.json_encode(explode(',',$nb_cpf_settings_options['onlyCountries'])).',';
       	}else{
       		$onlyCountries = '';
       	}
       	if(isset( $nb_cpf_settings_options['preferredCountries'] ) && $nb_cpf_settings_options['preferredCountries'] !=''){
       		$preferredCountries = 'preferredCountries: '.json_encode(explode(',',$nb_cpf_settings_options['preferredCountries'])).',';
       	}else{
       		$preferredCountries = '';
       	}
       	if(isset( $nb_cpf_settings_options['excludeCountries'] ) && $nb_cpf_settings_options['excludeCountries'] !=''){
       		$excludeCountries = 'excludeCountries: '.json_encode(explode(',',$nb_cpf_settings_options['excludeCountries'])).',';
       	}else{
       		$excludeCountries = '';
       	}
   
       	// phone field settings
   
       	if(isset( $nb_cpf_settings_options['phone_defaultCountry'] ) && $nb_cpf_settings_options['phone_defaultCountry'] !=''){
       		$phone_defaultCountry = 'initialCountry: "'.strtolower( $nb_cpf_settings_options['phone_defaultCountry'] ).'",';
       	} else {
       		$phone_defaultCountry = '';
   
       	}
       	if(isset( $nb_cpf_settings_options['phone_onlyCountries'] ) && $nb_cpf_settings_options['phone_onlyCountries'] !=''){
       		$phone_onlyCountries = 'onlyCountries: '.json_encode(explode(',',$nb_cpf_settings_options['phone_onlyCountries'])).',';
       	}else{
       		$phone_onlyCountries = '';
       	}
       	if(isset( $nb_cpf_settings_options['phone_preferredCountries'] ) && $nb_cpf_settings_options['phone_preferredCountries'] !=''){
       		$phone_preferredCountries = 'preferredCountries: '.json_encode(explode(',',$nb_cpf_settings_options['phone_preferredCountries'])).',';
       	}else{
       		$phone_preferredCountries = '';
       	}
       	if(isset( $nb_cpf_settings_options['phone_excludeCountries'] ) && $nb_cpf_settings_options['phone_excludeCountries'] !=''){
       		$phone_excludeCountries = 'excludeCountries: '.json_encode(explode(',',$nb_cpf_settings_options['phone_excludeCountries'])).',';
       	}else{
       		$phone_excludeCountries = '';
       	}
   
       	if(isset($nb_cpf_settings_options['phone_nationalMode']) && $nb_cpf_settings_options['phone_nationalMode'] == 1){
       		$phone_nationalMode = 'true';
       	}else {
       		$phone_nationalMode = 'false';
       	}
   
       	$custom_inline_js = '';
   
       	if(isset($phone_defaultCountry) && $phone_defaultCountry == ''){
       		$custom_inline_js .= '';
       	}
   
       	if( ( isset( $nb_cpf_settings_options['country_auto_select'] ) && $nb_cpf_settings_options['country_auto_select'] == 1 ) || ( isset( $nb_cpf_settings_options['phone_auto_select'] ) && $nb_cpf_settings_options['phone_auto_select'] == 1 ) ){
       		$custom_inline_js .= '
       		(function($) {
       			$(function() {
   
       				function render_country_flags(){
   
       					$(".wpcf7-countrytext").countrySelect({
       						'.$defaultCountry.''.$onlyCountries.''.$preferredCountries.''.$excludeCountries.'
       					});
       					$(".wpcf7-phonetext").intlTelInput({
       						autoHideDialCode: false,
       						autoPlaceholder: "off",
       						nationalMode: '.$phone_nationalMode.',
       						separateDialCode: false,
       						hiddenInput: "full_number",
       						'.$phone_defaultCountry.''.$phone_onlyCountries.''.$phone_preferredCountries.''.$phone_excludeCountries.'	
       					});
   
       					$(".wpcf7-phonetext").each(function () {
       						var hiddenInput = $(this).attr(\'name\');
       						//console.log(hiddenInput);
       						$("input[name="+hiddenInput+"-country-code]").val($(this).val());
       					});
   
       					$(".wpcf7-phonetext").on("countrychange", function() {
       						// do something with iti.getSelectedCountryData()
       						//console.log(this.value);
       						var hiddenInput = $(this).attr("name");
       						$("input[name="+hiddenInput+"-country-code]").val(this.value);
   
       					});';
   
       					if(! isset($nb_cpf_settings_options['phone_nationalMode']) || isset($nb_cpf_settings_options['phone_nationalMode']) && $nb_cpf_settings_options['phone_nationalMode'] != 1){
   
       						$custom_inline_js .= '$(".wpcf7-phonetext").on("keyup", function() {
       							var dial_code = $(this).siblings(".flag-container").find(".country-list li.active span.dial-code").text();
       							if(dial_code == "")
       							var dial_code = $(this).siblings(".flag-container").find(".country-list li.highlight span.dial-code").text();
       							var value   = $(this).val();
       							console.log(dial_code, value);
       							$(this).val(dial_code + value.substring(dial_code.length));
       						 });';
   
       					}
   
       					$custom_inline_js .= '$(".wpcf7-countrytext").on("keyup", function() {
       						var country_name = $(this).siblings(".flag-dropdown").find(".country-list li.active span.country-name").text();
       						if(country_name == "")
       						var country_name = $(this).siblings(".flag-dropdown").find(".country-list li.highlight span.country-name").text();
   
       						var value   = $(this).val();
       						//console.log(country_name, value);
       						$(this).val(country_name + value.substring(country_name.length));
       					});
       				}
   
       				var ip_address = "";
   
       				jQuery.ajax({
       					url: "https://api.ipgeolocation.io/getip",
       					success: function(response){
       						ip_address = response.ip;
       						//console.log(response);
       						var ajaxData = {
       							\'action\': \'auto_country_detection\',
       							\'ip\': ip_address,
       						};
       						jQuery.ajax( { 
       							url : nbcpf.ajaxurl,
       							data: ajaxData,
       							type: "GET", 
       							success: function(response) {
       								//console.log(response);
       								var location = JSON.parse(response);
       								//console.log(location);
       								if( location.country_code !== undefined){
       									//console.log("here");
       									$(".wpcf7-countrytext").countrySelect({';
   
       									$custom_inline_js .= isset( $nb_cpf_settings_options['country_auto_select'] ) 
       									&& $nb_cpf_settings_options['country_auto_select'] == 1 
       									? 'defaultCountry: location.country_code.toLowerCase(),' : '';
   
       									$custom_inline_js .= $onlyCountries.''.$preferredCountries.''.$excludeCountries.'
       									});
       									$(".wpcf7-phonetext").intlTelInput({
       										autoHideDialCode: false,
       										autoPlaceholder: "off",
       										nationalMode: '.$phone_nationalMode.',
       										separateDialCode: false,
       										hiddenInput: "full_number",';
       									$custom_inline_js .= isset( $nb_cpf_settings_options['phone_auto_select'] ) 
       									&& $nb_cpf_settings_options['phone_auto_select'] == 1 ?
       										'initialCountry: location.country_code.toLowerCase(),' : '';
       									$custom_inline_js .= $phone_onlyCountries.''.$phone_preferredCountries.''.$phone_excludeCountries.'	
       									});
   
       									$(".wpcf7-phonetext").each(function () {
       										var hiddenInput = $(this).attr(\'name\');
       										//console.log(hiddenInput);
       										$("input[name="+hiddenInput+"-country-code]").val($(this).val());
       									});
   
       									$(".wpcf7-phonetext").on("countrychange", function() {
       										// do something with iti.getSelectedCountryData()
       										//console.log(this.value);
       										var hiddenInput = $(this).attr("name");
       										$("input[name="+hiddenInput+"-country-code]").val(this.value);
   
       									});';
   
       									if(! isset($nb_cpf_settings_options['phone_nationalMode']) || isset($nb_cpf_settings_options['phone_nationalMode']) && $nb_cpf_settings_options['phone_nationalMode'] != 1){
   
       										$custom_inline_js .= '$(".wpcf7-phonetext").on("keyup", function() {
       											var dial_code = $(this).siblings(".flag-container").find(".country-list li.active span.dial-code").text();
       											if(dial_code == "")
       											var dial_code = $(this).siblings(".flag-container").find(".country-list li.highlight span.dial-code").text();
       											var value   = $(this).val();
       											console.log(dial_code, value);
       											$(this).val(dial_code + value.substring(dial_code.length));
       										});';
   
       									}
   
       									$custom_inline_js .= '$(".wpcf7-countrytext").on("keyup", function() {
       										var country_name = $(this).siblings(".flag-dropdown").find(".country-list li.active span.country-name").text();
       										if(country_name == "")
       										var country_name = $(this).siblings(".flag-dropdown").find(".country-list li.highlight span.country-name").text();
   
       										var value   = $(this).val();
       										//console.log(country_name, value);
       										$(this).val(country_name + value.substring(country_name.length));
       									});
   
       								} else {
   
       									render_country_flags();
   
       								}
   
       							},
       							error: function(){
       								render_country_flags();
       							}
       						})
       					}, error: function(){
       						render_country_flags();
       					}
       				});
       			});
       		})(jQuery);';
   
       	}else{ 
   
       		$custom_inline_js .= '
       		(function($) {
       			$(function() {
       				$(".wpcf7-countrytext").countrySelect({
       					'.$defaultCountry.''.$onlyCountries.''.$preferredCountries.''.$excludeCountries.'
       				});
       				$(".wpcf7-phonetext").intlTelInput({
       					autoHideDialCode: false,
       					autoPlaceholder: "off",
       					nationalMode: '.$phone_nationalMode.',
       					separateDialCode: false,
       					hiddenInput: "full_number",
       					'.$phone_defaultCountry.''.$phone_onlyCountries.''.$phone_preferredCountries.''.$phone_excludeCountries.'	
       				});
   
       				$(".wpcf7-phonetext").each(function () {
       					var hiddenInput = $(this).attr(\'name\');
       					//console.log(hiddenInput);
       					$("input[name="+hiddenInput+"-country-code]").val($(this).val());
       				});
   
       				$(".wpcf7-phonetext").on("countrychange", function() {
       					// do something with iti.getSelectedCountryData()
       					//console.log(this.value);
       					var hiddenInput = $(this).attr("name");
       					$("input[name="+hiddenInput+"-country-code]").val(this.value);
   
       				});';
   
       				if(! isset($nb_cpf_settings_options['phone_nationalMode']) || isset($nb_cpf_settings_options['phone_nationalMode']) && $nb_cpf_settings_options['phone_nationalMode'] != 1){
   
       					$custom_inline_js .= '$(".wpcf7-phonetext").on("keyup", function() {
       						var dial_code = $(this).siblings(".flag-container").find(".country-list li.active span.dial-code").text();
       						if(dial_code == "")
       						var dial_code = $(this).siblings(".flag-container").find(".country-list li.highlight span.dial-code").text();
       						var value   = $(this).val();
       						console.log(dial_code, value);
       						$(this).val(dial_code + value.substring(dial_code.length));
       					 });';
   
       				}
   
       				$custom_inline_js .= '$(".wpcf7-countrytext").on("keyup", function() {
       					var country_name = $(this).siblings(".flag-dropdown").find(".country-list li.active span.country-name").text();
       					if(country_name == "")
       					var country_name = $(this).siblings(".flag-dropdown").find(".country-list li.highlight span.country-name").text();
   
       					var value   = $(this).val();
       					//console.log(country_name, value);
       					$(this).val(country_name + value.substring(country_name.length));
       				});
   
       			});
       		})(jQuery);';
   
       	}
   
   
       	wp_add_inline_script('nbcpf-countryFlag-script',$custom_inline_js );
   
       }
   
       add_action( 'wp_enqueue_scripts', 'nb_cpf_embedCssJs' );
   
   
       add_action('wp_ajax_nopriv_auto_country_detection', 'nb_cpf_autoCountryDetection');
       add_action('wp_ajax_auto_country_detection', 'nb_cpf_autoCountryDetection' );
   
       function nb_cpf_autoCountryDetection(){
   
       	$nb_cpf_settings_options = get_option( 'nb_cpf_options' );
   
       	$api_key = isset($nb_cpf_settings_options['ip_api_key']) && $nb_cpf_settings_options['ip_api_key'] != '' ? $nb_cpf_settings_options['ip_api_key'] : '3abce2be42d640a8a98e82806e32cd4f';
       	//$api_key = '3abce2be42d640a8a98e82806e32cd4f';
       	//$api_url = "https://api.ipgeolocation.io/ipgeo?apiKey=".$api_key.'&fields=country_code2,country_name';
   
       	$ip_address = $_REQUEST['ip'];
       	if($ip_address != ''){
       		$api_url = 'https://ipwho.is/'.$ip_address;
       		$response = wp_safe_remote_get(
       			$api_url,
       			array(
       				'timeout' => 3,
       			)
       		);
       		//print_r($response);
       		$response = wp_remote_retrieve_body( $response );
   
   
       		if ( is_wp_error( $response ) ) {
       				echo '<script>console.log("'. $response->get_error_message(); '"); </script>'; 
       			return false; //$error_message = $response->get_error_message();
   
       		} else {
   
       			$parse_json = json_decode($response, true);
       			//print_r($parse_json);
       			echo json_encode($parse_json);
       			//$api_data = json_decode( $response['body'], true );
       		}
   
   
       	} else {
       		return false;
       	}
   
       	wp_die();
   
   
       }
       ```
   
 *  [meshackcris](https://wordpress.org/support/users/meshackcris/)
 * (@meshackcris)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/country-code-or-dial-code-getting-failed-to-load/#post-16392317)
 * replace the includ-js-css.php with this code
 *  Plugin Author [Narinder Singh](https://wordpress.org/support/users/narinderbisht/)
 * (@narinderbisht)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/country-code-or-dial-code-getting-failed-to-load/#post-16395497)
 * [@meshackcris](https://wordpress.org/support/users/meshackcris/) Thanks a lot
   for sharing your code. 
   [@prabhjas](https://wordpress.org/support/users/prabhjas/)
   Yes, the IP API URL was not working. As you can find [@meshackcris](https://wordpress.org/support/users/meshackcris/)
   share a new API URL “[https://ipwho.is/&#8221](https://ipwho.is/&#8221);. It 
   is working fine. Please update the plugin code. I was busy so not able to reply
   promptly.I hope the plugin is working fine for you. Kindly share reviews.RegardsNarinder.
 *  [riyashah19](https://wordpress.org/support/users/riyashah19/)
 * (@riyashah19)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/country-code-or-dial-code-getting-failed-to-load/#post-16398419)
 * Hello,
 * Thanks for sharing the code.
 * But default country is not working correctly. Can you please give an update to
   this plugin with working code?
 *  Plugin Author [Narinder Singh](https://wordpress.org/support/users/narinderbisht/)
 * (@narinderbisht)
 * [3 years, 3 months ago](https://wordpress.org/support/topic/country-code-or-dial-code-getting-failed-to-load/#post-16540484)
 * Hello [@all](https://wordpress.org/support/users/all/), [@riyashah19](https://wordpress.org/support/users/riyashah19/)
   
   I hope the plugin is working fine for all. I updated the new IP tracking API.
   Kindly update the plugin.
 * I hope you all enjoying the plugin. Kindly share your valuable reviews.
   I am 
   marking this thread resolved.
 * 
   Best RegardsNarinder.

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/country-code-or-dial-code-getting-failed-to-load/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/country-code-or-dial-code-getting-failed-to-load/page/2/?output_format=md)

The topic ‘Country Code or Dial Code getting failed to load’ is closed to new replies.

 * ![](https://ps.w.org/country-phone-field-contact-form-7/assets/icon-256x256.png?
   rev=2064274)
 * [Country & Phone Field Contact Form 7](https://wordpress.org/plugins/country-phone-field-contact-form-7/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/country-phone-field-contact-form-7/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/country-phone-field-contact-form-7/)
 * [Active Topics](https://wordpress.org/support/plugin/country-phone-field-contact-form-7/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/country-phone-field-contact-form-7/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/country-phone-field-contact-form-7/reviews/)

## Tags

 * [api](https://wordpress.org/support/topic-tag/api/)
 * [fail](https://wordpress.org/support/topic-tag/fail/)

 * 16 replies
 * 8 participants
 * Last reply from: [prabhjas](https://wordpress.org/support/users/prabhjas/)
 * Last activity: [3 years, 3 months ago](https://wordpress.org/support/topic/country-code-or-dial-code-getting-failed-to-load/page/2/#post-16540585)
 * Status: resolved