Back to Tips & Tricks

How to Auto-Detect Country and Phone Dial Code in JetFormBuilder

Published on 24 August 2025

If you’re using JetFormBuilder to build forms in WordPress, you may want your phone field to automatically detect a visitor’s country and apply the correct international dialing code. This not only improves user experience but also ensures you collect properly formatted phone numbers for leads or customers.

In this guide, I’ll walk you through a working solution using custom PHP + JavaScript code that integrates seamlessly with JetFormBuilder.


What This Code Does

  • Adds an international telephone input to your form.
  • Auto-detects the visitor’s country using their IP address.
  • Inserts the correct country dial code into the phone field.
  • Updates a second field with the detected country name.
  • Uses WordPress hooks so the scripts load only where needed (like your Contact page).


Create Fields in JetFormBuilder

Inside your JetFormBuilder form:

  1. Phone Field
    • Type: Text → set as TEL
    • Field name: phone
    • CSS class: phone-class
  2. Country Field
    • Type: Text
    • Field name: country

👉 These two fields will work together to capture both the number and the detected country.


Get Your Free API Key

To detect user location by IP, sign up at ipinfo.io and grab a free token.

You’ll need to replace it in two places in the code:

  • In PHP ($api_key = 'YOUR TOKEN KEY';)
  • In JavaScript (fetch('https://ipinfo.io?token=YOUR API KEY'))


Add the Custom Code

Add this code to your functions.php (child theme) or a custom plugin:


How to Auto-Detect Country and Phone Dial Code in JetFormBuilder - BoostMyCroco