Skip to content

Newsletter sign-up form in WordPress

An e-mail field and its button on one line, a GDPR consent checkbox, clear errors, a typo suggestion and a confirmation message.

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

formulaire-newsletter-inline.php
<?php
/**
 * Plugin Name: Forge, Formulaire d'inscription à la newsletter
 * Description: Un champ e-mail et son bouton sur une ligne, une case de consentement RGPD, des erreurs claires, une suggestion en cas de faute de frappe et un message de confirmation. Ajoute la composition « Formulaire d'inscription à la newsletter » à 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-formulaire-newsletter-inline
 *
pattern.html
<!-- wp:html -->
<section class="nwl" aria-labelledby="nwl-titre" data-nwl>
        <div class="nwl__head">
          <p class="nwl__eyebrow">Le Potager des Monts d'Or</p>
          <h2 class="nwl__title" id="nwl-titre">La lettre de la récolte</h2>
          <p class="nwl__intro">Chaque premier mardi du mois : les légumes qui arrivent sur l'étal, une recette de saison et les dates des marchés.</p>
        </div>
        <ul class="nwl__perks" aria-label="Ce que contient la lettre">
          <li class="nwl__perk">
            <svg class="nwl__perk-icon" viewBox="0 0 20 20" width="20" height="20" aria-hidden="true" focusable="false"><path d="M5 10.5l3 3 7-7" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
            Une lettre par mois
          </li>
script.js
(() => {
  if (!document.querySelector(".nwl")) return;
  const NWL_DOMAINS = ["gmail.com", "hotmail.fr", "hotmail.com", "outlook.fr", "outlook.com", "yahoo.fr", "yahoo.com", "orange.fr", "free.fr", "sfr.fr", "laposte.net", "wanadoo.fr", "icloud.com"];

  // Edit distance with adjacent swaps, so "gmial.com" counts as one typo away from "gmail.com".
  function nwlDistance(a, b) {
    const d = Array.from({ length: a.length + 1 }, (_, i) => [i, ...Array(b.length).fill(0)]);
    for (let j = 1; j <= b.length; j += 1) d[0][j] = j;
    for (let i = 1; i <= a.length; i += 1) {
      for (let j = 1; j <= b.length; j += 1) {
        const cost = a[i - 1] === b[j - 1] ? 0 : 1;
        d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
style.css
:root {
  --nwl-card: #ffffff;
  --nwl-text: #14261b;
  --nwl-muted: #4a5d50;
  --nwl-line: #d5e3d8;
  --nwl-line-strong: #6f8576;
  --nwl-field: #ffffff;
  --nwl-accent: #166534;
  --nwl-accent-hover: #14532d;
  --nwl-accent-soft: #dcfce7;
  --nwl-on-accent: #ffffff;
  --nwl-error: #b91c1c;

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