• Resolved Ali Abbas

    (@ali199818)


    hello, i use this code th change country on address filed based on user ip using ipapi API, but does not work

    this is the code

    add_filter( 'forminator_field_address_markup', 'wpmudev_forminator_get_country_ip', 10, 5 );
    function wpmudev_forminator_get_country_ip( $html, $field){
    	
    	$user_ip = Forminator_Geo::get_user_ip();
    	
    	$country = wpmudev_forminator_set_country( $user_ip );
        
    	if( $country ){
    		$html = str_replace('field=""',"field=$country", $html);
    	}
    	
    	return $html;
    }
    
    function wpmudev_forminator_set_country( $user_ip ) {
    	
    	
    	$curl = curl_init();
    	
        curl_setopt_array($curl, array(
          CURLOPT_URL => 'https://ipapi.co/'.$user_ip.'/country_name/',
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => '',
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => 'GET',
        ));
        
        $response = curl_exec($curl);
        
        curl_close($curl);
    	
    	
    	return $response;
    }

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @ali199818

    I hope you’re well today!

    This code is only partially correct. it does get the IP and gets the country out of it but it won’t correctly select it.

    The main issue here is this line:

    $html = str_replace('field=""',"field=$country", $html);

    which doesn’t really set anything “meaningful”.

    Instead, you’d need to set default option value, remove “selected” flag for option already selected by default and then set it for matching country.

    So in the code please replace above line with this three lines:

    $html = str_replace('data-default-value=""','data-default-value="'.$country.'"', $html);
    $html = str_replace( 'selected>', '', $html);
    $html = str_replace( 'value="'.$country.'"', 'value="'.$country.'" selected', $html);

    This should do the trick and works fine in my tests.

    Best regards,
    Adam

    Thread Starter Ali Abbas

    (@ali199818)

    thank you so muck it wroks, but one more question how can i do it for city?

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @ali199818

    I’m glad I could help!

    As for the city, it should be possible with a bit of modification of the code. Here is a new version:

     
    
    add_filter( 'forminator_field_address_markup', 'wpmudev_forminator_get_country_ip', 10, 5 );
    function wpmudev_forminator_get_country_ip( $html, $field){
    	
    	$user_ip = Forminator_Geo::get_user_ip();
    	
    	$ip_data = json_decode( wpmudev_forminator_set_ip_data( $user_ip ), true );
    	
    	if ( $ip_data['error'] < 1 ) { 
    		
    		$country = $ip_data['country_name'];
    		$city = $ip_data['city'];
    	
    	    
    		if( $country ) {
    			$html = str_replace('data-default-value=""','data-default-value="'.$country.'"', $html);
    			$html = str_replace( 'selected>', '', $html);
    			$html = str_replace( 'value="'.$country.'"', 'value="'.$country.'" selected', $html);
    		}
    		
    		if ( $city ) {
    			$html = str_replace( 'value=""', '', $html );
    			$html = str_replace( 'name="address-1-city"', 'name="address-1-city" value="'.$city.'" ', $html );
    		}
    		
    	}
    	
    	return $html;
    }
    
    function wpmudev_forminator_set_ip_data( $user_ip ) {
    	
    	
    	$curl = curl_init();
    	
        curl_setopt_array($curl, array(
          CURLOPT_URL => 'https://ipapi.co/'.$user_ip.'/json/',
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => '',
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => 'GET',
        ));
        
        $response = curl_exec($curl);
        
        curl_close($curl);
    	
    	
    	return $response;
    }

    Just a note though: it seems I went under the rate limit of a free version of that IP API (it’s a 3rd-party solution) so I could only test it on “dumb” data. It should be working but you need to test it on your end.

    Best regards,
    Adam

    Thread Starter Ali Abbas

    (@ali199818)

    No, it does not work also from my end (the API is work on other projects, i test it), I think there is an error in code

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @ali199818,

    Could you please share the form export where you are testing the given code, so that we could check further?

    Please check the following doc on how to export a form:
    https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-export

    If you are concerned about any sensitive information in the form, then you can duplicate your form, remove any sensitive information, and then export it.

    You can share the export file via Google Drive, Dropbox or any cloud services in the next reply.

    Looking forward to your response.

    Best Regards,

    Nithin

    Thread Starter Ali Abbas

    (@ali199818)

    this is the form

    Link

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @ali199818,

    Thanks for sharing the export, I’m checking with our developer regarding this and will get back to you once we get further feedback asap.

    Kind Regards,

    Nithin

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @ali199818,

    Unfortunately we are getting rate limit error, so we couldn’t fully test it out to state what might be causing the “City” to not work.

    Could you please update the snippet by adding print_r($ip_data); after the following line:

    $ip_data = json_decode( wpmudev_forminator_set_ip_data( $user_ip ), true );

    The above change should provide the output of the data passed to $ip_data variable after the submission in order to have a better idea of what might be causing it.

    You can share the output in the next reply using Google Drive, Dropbox, pastebin or any such cloud services so that we can check further.

    Looking forward to your response.

    Kind Regards,

    Nithin

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Country Change based on user ip’ is closed to new replies.