Skip to content

Filterable gallery in WordPress

A photo grid filtered by theme, with toggle buttons and an announcement of how many photos are shown.

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-filtrable.php
<?php
/**
 * Plugin Name: Forge, Galerie filtrable
 * Description: Une grille de photos filtrée par thème, avec des boutons à bascule et l'annonce du nombre de photos affichées. Ajoute la composition « Galerie filtrable » à 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-filtrable
 *
pattern.html
<!-- wp:html -->
<section class="pgal" aria-labelledby="pgal-title" data-filter-gallery>
        <div class="pgal__head">
          <h2 id="pgal-title">Portfolio</h2>
          <div class="pgal__filters" role="group" aria-label="Filtrer par thème" data-filter-group hidden>
          <button type="button" class="pgal__filter" data-filter="all" aria-pressed="true">Tout</button>
          <button type="button" class="pgal__filter" data-filter="mariages" aria-pressed="false">Mariages</button>
          <button type="button" class="pgal__filter" data-filter="portraits" aria-pressed="false">Portraits</button>
          <button type="button" class="pgal__filter" data-filter="paysages" aria-pressed="false">Paysages</button>
          </div>
        </div>
        <p class="pgal__status" role="status" data-filter-status></p>
script.js
(() => {
  if (!document.querySelector(".pgal")) return;
  document.querySelectorAll("[data-filter-gallery]").forEach((root) => {
    const buttons = [...root.querySelectorAll("[data-filter]")];
    const items = [...root.querySelectorAll("[data-category]")];
    const status = root.querySelector("[data-filter-status]");

    function apply(button) {
      const value = button.dataset.filter;
      let shown = 0;
      items.forEach((item) => {
        const match = value === "all" || item.dataset.category === value;
style.css
:root {
  --pg-text: #3b0a1f;
  --pg-muted: #7a4359;
  --pg-chip: #ffffff;
  --pg-line: #efc3d2;
  --pg-active: #be185d;
  --pg-active-text: #ffffff;
  --pg-ring: #9d174d;
  --demo-galerie-filtrable-bg: #fff7f9;
  --demo-galerie-filtrable-text: #3b0a1f;
}

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