Skip to content

Image carousel in WordPress

A photo carousel with previous, next, dots and a pause button, which never advances on its own when the user reduces motion.

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

carrousel-images.php
<?php
/**
 * Plugin Name: Forge, Carrousel d'images
 * Description: Un carrousel de photos avec précédent, suivant, puces et bouton pause, qui ne défile jamais tout seul si l'utilisateur limite les animations. Ajoute la composition « Carrousel d'images » à 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-carrousel-images
 *
pattern.html
<!-- wp:html -->
<section class="pcar" aria-roledescription="carrousel" aria-label="Photos du Mas des Oliviers" data-carousel>
        <div class="pcar__slides" id="pcar-slides" data-carousel-slides>
          <figure class="pcar__slide" role="group" aria-roledescription="diapositive" aria-label="1 sur 4" data-slide>
            <div class="pcar__photo" role="img" aria-label="Terrasse en pierre avec table dressée face aux collines au coucher du soleil" style="background: linear-gradient(135deg,#fcd9a8,#c2410c)"></div>
            <figcaption><h3>La terrasse au soleil couchant</h3><p>Dîner face aux Alpilles, de mai à septembre.</p></figcaption>
          </figure>
          <figure class="pcar__slide" role="group" aria-roledescription="diapositive" aria-label="2 sur 4" data-slide>
            <div class="pcar__photo" role="img" aria-label="Chambre aux murs mauves, lit blanc et fenêtre ouverte sur le jardin" style="background: linear-gradient(135deg,#e2d5f5,#6d4fa8)"></div>
            <figcaption><h3>Chambre Lavande</h3><p>Lit de 160, salle d'eau privative et vue sur le jardin.</p></figcaption>
          </figure>
          <figure class="pcar__slide" role="group" aria-roledescription="diapositive" aria-label="3 sur 4" data-slide>
script.js
(() => {
  if (!document.querySelector(".pcar")) return;
  document.querySelectorAll("[data-carousel]").forEach((root) => {
    const slides = [...root.querySelectorAll("[data-slide]")];
    const dots = [...root.querySelectorAll("[data-carousel-dot]")];
    const track = root.querySelector("[data-carousel-slides]");
    const toggle = root.querySelector("[data-carousel-toggle]");
    let index = 0;
    let playing = !window.matchMedia("(prefers-reduced-motion: reduce)").matches;
    let hovered = false;
    let timer;
style.css
:root {
  --pc-text: #042f2e;
  --pc-muted: #3f6260;
  --pc-card: #ffffff;
  --pc-line: #bfe3de;
  --pc-accent: #0f766e;
  --pc-dot: #8fbfb9;
  --pc-ring: #134e4a;
  --pc-shadow: 0 10px 30px -18px rgb(4 47 46 / 0.5);
  --demo-carrousel-images-bg: #f0fbfa;
  --demo-carrousel-images-text: #042f2e;
}

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