[Forminator Pro] Phone number field country flag selected using user’s geolocation

1

I am using your plugin’s free version and it is really good one.
But for phone number field, i need help.
Phone number field has country’s flag. I want to keep it selected and add country code in that field from user’s geolocation. is there any hook or filters your plugin have?

  • Adam
    • Support Gorilla

    Hi RS

    I hope you’re well today!

    Currently there’s no such option built-in into plugin. It sounds like a useful idea so since you’ve posted this already in our Feature Requests forum, let’s keep this ticket open so other Member could also vote for it and share their feedback.

    Our Forminator Team is closely monitoring this space so if if this idea gains some interest it’s very likely to get implemented with one of future releases.

    As for workaround for now: I checked and there seems to be no hook that could be used “directly” for that. There are hooks that would let you alter field’s HTML but that’s not as simple as it seems due to how the form works (and you’d most likely need to, kind of, “rebuild” field markup) so it doesn’t sound like the best solution, I think.

    But I have asked our Second Line Support about it. I might have missed something (as non-developer) so they’ll check and see if there’s some other/better way to hook to that part of field generation and if it would be possible then to somehow set that field’s flag/country code “on the fly”.

    I’d appreciate some patience on this as our developers are dealing with a lot of complex tasks on daily basis and their response time may be a bit longer than ours here on forum but if what you are asking for is doable and relatively simple, we’ll update you here.

    Best regards,
    Adam

  • Adam
    • Support Gorilla

    Hi RS

    I just got feedback from one of our developers on possible custom solution. It turns out it was way simpler than I expected. It’s still not quite a solution to directly implement in the plugin but it should be fine

    – as a workaround until feature is possibly implemented
    – as a based for your own custom solution

    This is the code:

    <?php 
    
    add_filter( 'forminator_field_phone_markup', 'wpmudev_forminator_get_country_code_ip', 10, 5 );
    function wpmudev_forminator_get_country_code_ip( $html, $id, $required, $placeholder, $value ){
    	
    	$user_ip = Forminator_Geo::get_user_ip();
    	
    	$country_code = wpmudev_forminator_set_country_code( $user_ip );
        
    	if( $country_code ){
    		$html = str_replace('value=""',"value=$country_code", $html);
    	}
    	
    	return $html;
    }
    
    function wpmudev_forminator_set_country_code( $user_ip ) {
    	
    	
    	/* IP TO COUNTRY CODE
    	YOU CAN REPLACE THIS WITH YOUR OWN 
    	CODE 
    	
    	IT HAS TO RETURN STANDARIZED CONTRY CODE
    	
    	*/ 
    	
    	$curl = curl_init();
    	
        curl_setopt_array($curl, array(
          CURLOPT_URL => 'https://ipapi.co/'.$user_ip.'/country_calling_code/',
          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;
    }

    To use it on site, you’d need to add it as an MU plugin:

    – create an empty file with a .php extension (e.g. “forminator-phone-auto-country.php”)
    – copy and paste code into it and save the file
    – upload the file (using FTP, cPanel “File Manager” or similar way) to the “/wp-content/mu-plugins” folder of the site.

    The phone field on the form should be set to use “International” validation and it should work out of the box then.

    There is, however, a major issue to consider: while Forminator provides user IP (to be then used for country detection), the actual country detection requires use of some kind of API. The most popular would actually by MaxMind database but that requires more and quite a complex implementation. Another option is to use some external API.

    In this code we just use “ipapi.com” service in free version (no authorization etc) so while it works I’m not entirely sure how reliable accurate and reliable it is. It seems to be working fine in my tests but I can’t give any guarantee on that.

    There’s also a matter of possible request quotas. It’s quite possible that if there’s more requests, they might start throttling or blocking them until you pay for service. I think it’s 1000 “checks” a month with them currently. But then – turning into paid version would also require some modification of the code for it to actually authorize. That’s out of the scope of this support.

    To sum it up:

    – the code that I shared works but may be of limited use due to IP geo-lookup service used
    – for some more “serious” use you would need to develop/customize your own IP to Country check code – so basically change/custom-develop the “wpmudev_forminator_set_country_code()” function of that shared code; this function must return a proper and standard country code (e.g. PL for Poland, DE for Germany, PT for Portugal and so on).

    Best regards,
    Adam