Skip to content

Light/dark theme switch in WordPress

An accessible switch that turns the whole site dark or light through CSS variables, remembers the choice and returns to the device setting in one click.

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

interrupteur-bascule-clair-sombre.php
<?php
/**
 * Plugin Name: Forge, Interrupteur de thème clair/sombre
 * Description: Une bascule accessible qui passe tout le site en mode sombre ou clair par variables CSS, mémorise le choix et revient d’un clic au réglage de l’appareil. Ajoute la composition « Interrupteur de thème clair/sombre » à 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-interrupteur-bascule-clair-sombre
 *
pattern.html
<!-- wp:html -->
<section class="itcs-card" aria-labelledby="itcs-titre" data-itcs>
        <header class="itcs-head">
          <p class="itcs-brand">
            <span class="itcs-logo" aria-hidden="true">AB</span>
            <span class="itcs-names"><strong>Atelier Brune</strong><span class="itcs-place">Céramique à Lyon</span></span>
          </p>
          <button class="itcs-switch" type="button" aria-pressed="false" data-itcs-toggle hidden>
            <span class="itcs-label">Mode sombre</span>
            <span class="itcs-track" aria-hidden="true">
              <span class="itcs-thumb"><svg class="itcs-sun" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="4"/><path d="M12 2.5v2M12 19.5v2M4.6 4.6 6 6M18 18l1.4 1.4M2.5 12h2M19.5 12h2M4.6 19.4 6 18M18 6l1.4-1.4"/></svg><svg class="itcs-moon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M20 14.5A8 8 0 0 1 9.5 4a8 8 0 1 0 10.5 10.5Z"/></svg></span>
            </span>
script.js
(() => {
  if (!document.querySelector(".itcs-card")) return;
  const itcsKey = "itcs-theme";
  const itcsRoot = document.documentElement;
  const itcsSystem = window.matchMedia("(prefers-color-scheme: dark)");
  // Private browsing can block storage: the choice then lasts for the page only.
  const itcsRead = () => {
    try {
      const value = localStorage.getItem(itcsKey);
      return value === "dark" || value === "light" ? value : null;
    } catch {
      return null;
style.css
:root {
  --itcs-card-bg: #ffffff;
  --itcs-text: #1f2430;
  --itcs-muted: #4f5868;
  --itcs-line: #e3e6ec;
  --itcs-logo-bg: #f3ece4;
  --itcs-logo-text: #7c4a1e;
  --itcs-accent: #4338ca;
  --itcs-track-off: #eef0f4;
  --itcs-track-line: #6b7280;
  --itcs-thumb-off: #4b5563;
  --itcs-thumb-off-icon: #ffffff;

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