Skip to content

Lost connection alert in WordPress

A banner that warns visitors when they lose the network and announces when it is back, using browser events, with no request.

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

alerte-connexion-perdue.php
<?php
/**
 * Plugin Name: Forge, Alerte de connexion perdue
 * Description: Un bandeau qui prévient le visiteur quand il perd le réseau et lui annonce son retour, grâce aux événements du navigateur, sans requête. Ajoute la composition « Alerte de connexion perdue » à 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-alerte-connexion-perdue
 *
pattern.html
<!-- wp:html -->
<section class="acp" data-acp aria-labelledby="acp-titre">
        <div class="acp-banner" role="status" data-acp-banner hidden>
          <svg class="acp-icon" viewBox="0 0 24 24" width="22" height="22" aria-hidden="true"><path d="M2 8.5a15 15 0 0 1 20 0M5 12a10 10 0 0 1 14 0M8.5 15.5a5 5 0 0 1 7 0M12 19h.01M3 3l18 18" data-acp-icon-off/><path d="M2 8.5a15 15 0 0 1 20 0M5 12a10 10 0 0 1 14 0M8.5 15.5a5 5 0 0 1 7 0M12 19h.01" data-acp-icon-on/></svg>
          <p class="acp-msg" data-acp-msg></p>
          <button class="acp-close" type="button" aria-label="Fermer le message de connexion" data-acp-close>×</button>
        </div>
        <h2 class="acp-title" id="acp-titre">Votre espace adhérent</h2>
        <p class="acp-text">Inscriptions aux sorties, cotisation et documents du club de randonnée des Monts du Lyonnais.</p>
        <p class="acp-hint">Pour essayer, coupez le Wi-Fi ou passez en mode avion : le bandeau apparaît en haut de cette section, puis annonce le retour de la connexion.</p>
      </section>
<!-- /wp:html -->
script.js
(() => {
  if (!document.querySelector(".acp")) return;
  document.querySelectorAll("[data-acp]").forEach((root) => {
    const on = (el, type, fn) => el.addEventListener(type, fn);
    const offs = [];
    const banner = root.querySelector("[data-acp-banner]");
    const msg = root.querySelector("[data-acp-msg]");
    let timer = 0;

    const show = (online) => {
      window.clearTimeout(timer);
      banner.dataset.acpState = online ? "online" : "offline";
style.css
:root {
  --acp-card: #ffffff;
  --acp-text: #1c2321;
  --acp-muted: #4d5754;
  --acp-soft: #f2f5f4;
  --acp-off-bg: #fff7ed;
  --acp-off-line: #b45309;
  --acp-off-text: #7c2d12;
  --acp-on-bg: #f0fdf4;
  --acp-on-line: #15803d;
  --acp-on-text: #14532d;
  --demo-alerte-connexion-perdue-bg: #e9eeec;

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

Toasts and alerts components in WordPress
  • A bell with an unread badge that opens a notification panel: icons by type, read and unread items told apart, and a "Mark all as read" button.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • The four classic interface messages: information, success, warning and error.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • Three callouts for technical documentation: note, tip and warning.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A notice warning of a scheduled service interruption, with date, time and impact.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A dashed callout highlighting an offer and its code, with the end date.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • An announcement bar at the top of the page with a link and a button to close it.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component