Skip to content

Photo gallery with lightbox in WordPress

A photo mosaic (dishes, dining room, terrace) that opens in a large lightbox with captions, a thumbnail strip, keyboard navigation and swipe.

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

galerie-photos-lightbox.php
<?php
/**
 * Plugin Name: Forge, Galerie photos avec agrandissement
 * Description: Une mosaïque de photos (plats, salle, terrasse) qui s'ouvrent en grand dans une visionneuse avec légende, bande de vignettes, navigation au clavier et balayage du doigt. Ajoute la composition « Galerie photos avec agrandissement » à 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-galerie-photos-lightbox
 *
pattern.html
<!-- wp:html -->
<section class="glb" aria-labelledby="glb-title" data-glb>
        <div class="glb__head">
          <p class="glb__eyebrow">Bistrot Les Tilleuls · Nantes</p>
          <h2 id="glb-title">La maison en images</h2>
          <p class="glb__intro">Nos assiettes de saison, la salle et la terrasse sous les tilleuls. Touchez une photo pour l'agrandir.</p>
        </div>
        <ul class="glb__grid">
          <li class="glb__item glb__item--wide" data-glb-item>
            <figure class="glb__figure">
              <div class="glb__ph" data-glb-ph role="img" aria-label="Assiette blanche de poisson nappé de beurre blanc, vue de dessus sur une table vert sombre" style="background: radial-gradient(circle at 50% 52%, #f3e6c9 0 18%, #fbf8f1 19% 33%, #d9d2c3 34% 35%, transparent 36%), linear-gradient(135deg, #5b6b5d, #2f3a32)"></div>
              <figcaption class="glb__cap">
script.js
(() => {
  if (!document.querySelector(".glb")) return;
  document.querySelectorAll("[data-glb]").forEach((root) => {
    const items = [...root.querySelectorAll("[data-glb-item]")];
    const viewer = root.querySelector("[data-glb-viewer]");
    const photo = viewer.querySelector("[data-glb-photo]");
    const count = viewer.querySelector("[data-glb-count]");
    const title = viewer.querySelector("[data-glb-title]");
    const kind = viewer.querySelector("[data-glb-tag]");
    const text = viewer.querySelector("[data-glb-text]");
    const strip = viewer.querySelector("[data-glb-strip]");
    const model = viewer.querySelector("[data-glb-thumb]").content.firstElementChild;
style.css
:root {
  /* Rounded corners of the tiles: 0 for a strict grid, 28px for softer, organic shapes. */
  --glb-radius: 14px;
  --glb-text: #1f2a24;
  --glb-muted: #4f5d56;
  --glb-accent: #a63d1b;
  --glb-ring: #a63d1b;
  --demo-galerie-photos-lightbox-bg: #f7f4ee;
  --demo-galerie-photos-lightbox-text: #1f2a24;
}

@media (prefers-color-scheme: dark) {

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

Galleries and carousels components in WordPress
  • A product photo that zooms in under the pointer, and from the keyboard with Enter and the arrow keys, to inspect the details.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • Building site photos sorted by dated step on a vertical timeline, with the current step highlighted.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • YouTube videos shown as thumbnails: the player only loads on click, to respect privacy and keep the page light.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A slideshow that fills the whole screen, browsed with the keyboard or a finger swipe, and closed with Escape.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component