Skip to content

Address field with autocomplete in WordPress

A French address input with suggestions from the national address database (no API key), by keyboard or mouse, filling street, postcode and city.

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-adresse-autocompletion.php
<?php
/**
 * Plugin Name: Forge, Champ d’adresse avec autocomplétion
 * Description: Une saisie d’adresse française avec suggestions de la Base Adresse Nationale (sans clé d’API), au clavier comme à la souris, qui remplit voie, code postal et ville. Ajoute la composition « Champ d’adresse avec autocomplétion » à 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-adresse-autocompletion
 *
pattern.html
<!-- wp:html -->
<form class="fad" data-fad novalidate aria-labelledby="fad-titre">
        <h2 class="fad-title" id="fad-titre">Adresse de livraison</h2>
        <div class="fad-field">
          <label class="fad-label" for="fad-recherche">Rechercher une adresse</label>
          <div class="fad-combo">
            <input class="fad-input" id="fad-recherche" type="text" role="combobox" aria-autocomplete="list" aria-expanded="false" aria-controls="fad-liste" aria-describedby="fad-aide" autocomplete="off" spellcheck="false" placeholder="12 rue de la Paix, Lyon">
            <ul class="fad-list" id="fad-liste" role="listbox" aria-label="Adresses suggérées" hidden>
            <li class="fad-option" id="fad-opt-0" role="option" aria-selected="false" data-fad-option hidden></li>
            <li class="fad-option" id="fad-opt-1" role="option" aria-selected="false" data-fad-option hidden></li>
            <li class="fad-option" id="fad-opt-2" role="option" aria-selected="false" data-fad-option hidden></li>
            <li class="fad-option" id="fad-opt-3" role="option" aria-selected="false" data-fad-option hidden></li>
script.js
(() => {
  if (!document.querySelector(".fad")) return;
  document.querySelectorAll("[data-fad]").forEach((root) => {
    const on = (el, type, fn) => el.addEventListener(type, fn);
    const search = root.querySelector('[role="combobox"]');
    const list = root.querySelector('[role="listbox"]');
    const items = [...root.querySelectorAll("[data-fad-option]")];
    const help = root.querySelector("[data-fad-help]");
    const street = root.querySelector('[name="adresse"]');
    const zip = root.querySelector('[name="cp"]');
    const city = root.querySelector('[name="ville"]');
    const status = root.querySelector("[data-fad-status]");
style.css
:root {
  --fad-card: #ffffff;
  --fad-text: #1f1b2e;
  --fad-muted: #514a66;
  --fad-accent: #5b3cc4;
  --fad-line: #cfc8e3;
  --fad-soft: #f0ecfb;
  --fad-error: #b42318;
  --demo-champ-adresse-autocompletion-bg: #ece9f5;
  --demo-champ-adresse-autocompletion-text: #1f1b2e;
}

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