Skip to content

Phone field with country code in WordPress

A phone field with a country picker: the number is formatted while typing, its length is checked and it is sent in international format.

Here is an excerpt of this component's code in WordPress. The full code is free for members, ready to copy into your project.

This component also comes in

Open in a new tab

The code in WordPress

champ-telephone-indicatif.php
<?php
/**
 * Plugin Name: Forge, Téléphone avec indicatif pays
 * Description: Un champ téléphone avec choix du pays : le numéro se met en forme pendant la saisie, sa longueur est vérifiée et il est envoyé au format international. Ajoute la composition « Téléphone avec indicatif pays » à l'éditeur de blocs.
 * Version: 1.0.0
 * Requires at least: 6.6
 * Requires PHP: 8.1
 * Author: Forge
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: forge-champ-telephone-indicatif
 *
pattern.html
<!-- wp:html -->
<form class="cti" data-cti data-cti-rules="{&quot;FR&quot;:{&quot;code&quot;:&quot;33&quot;,&quot;groups&quot;:[2,2,2,2,2],&quot;lengths&quot;:[10],&quot;trunk&quot;:true,&quot;example&quot;:&quot;06 12 34 56 78&quot;},&quot;BE&quot;:{&quot;code&quot;:&quot;32&quot;,&quot;groups&quot;:[4,2,2,2],&quot;lengths&quot;:[9,10],&quot;trunk&quot;:true,&quot;example&quot;:&quot;0470 12 34 56&quot;},&quot;CH&quot;:{&quot;code&quot;:&quot;41&quot;,&quot;groups&quot;:[3,3,2,2],&quot;lengths&quot;:[10],&quot;trunk&quot;:true,&quot;example&quot;:&quot;079 123 45 67&quot;},&quot;LU&quot;:{&quot;code&quot;:&quot;352&quot;,&quot;groups&quot;:[3,3,3],&quot;lengths&quot;:[6,7,8,9],&quot;trunk&quot;:false,&quot;example&quot;:&quot;621 123 456&quot;},&quot;CA&quot;:{&quot;code&quot;:&quot;1&quot;,&quot;groups&quot;:[3,3,4],&quot;lengths&quot;:[10],&quot;trunk&quot;:false,&quot;example&quot;:&quot;514 555 0123&quot;}}" novalidate aria-labelledby="cti-titre">
        <h2 class="cti-title" id="cti-titre">Être rappelé par un conseiller</h2>
        <fieldset class="cti-fs">
          <legend class="cti-legend">Numéro de téléphone</legend>
          <div class="cti-row">
            <div class="cti-col cti-col--country">
              <label class="cti-small" for="cti-pays">Pays</label>
              <select class="cti-control" id="cti-pays" name="pays" autocomplete="tel-country-code">
                <option value="FR">France (+33)</option>
                <option value="BE">Belgique (+32)</option>
                <option value="CH">Suisse (+41)</option>
script.js
(() => {
  if (!document.querySelector(".cti")) return;
  document.querySelectorAll("[data-cti]").forEach((root) => {
    const on = (el, type, fn) => el.addEventListener(type, fn);
    const form = root;
    const rules = JSON.parse(form.dataset.ctiRules);
    const country = form.querySelector("#cti-pays");
    const input = form.querySelector("#cti-numero");
    const help = form.querySelector("[data-cti-help]");
    const e164 = form.querySelector("[data-cti-e164]");
    const status = form.querySelector("[data-cti-status]");
    const rule = () => rules[country.value];
style.css
:root {
  --cti-card: #ffffff;
  --cti-text: #1a2230;
  --cti-muted: #4a5466;
  --cti-line: #c8d0dc;
  --cti-field: #ffffff;
  --cti-accent: #3730a3;
  --cti-accent-text: #ffffff;
  --cti-error: #b91c1c;
  --demo-champ-telephone-indicatif-bg: #eceef5;
  --demo-champ-telephone-indicatif-text: #1a2230;
}

Free component: copy it freely, including for commercial projects.

Forms and inputs components in WordPress
  • A quote request form for tradespeople: the customer describes the work, picks a start date range and attaches up to six site photos, each with a preview, its size and a button to remove it.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A quote request form whose labels, hints and error messages are full French sentences, still readable at 320 px: nothing overflows, nothing is cut off, and each error is tied to its field.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • Visitors pick a package and tick options: the pre-tax total, VAT and total including tax update live, and the summary is sent with the contact form.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A short NPS survey: a 0 to 10 score as radio buttons, then a follow-up question adapted to whether the customer is a detractor, passive or promoter.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • An honest unsubscribe page: address prefilled from the link, choice between stopping everything or receiving less, optional reason and an undo.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A callback request form: French phone number checked and formatted, topic, working day and time slot, with explicit consent to the call.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component