Skip to content

Favourite button with counter in WordPress

Toggle heart buttons that count favourites, remember them between visits and announce them to screen readers.

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

bouton-favori-compteur.php
<?php
/**
 * Plugin Name: Forge, Bouton favori avec compteur
 * Description: Des boutons cœur à bascule qui comptent les favoris, s’en souviennent d’une visite à l’autre et l’annoncent aux lecteurs d’écran. Ajoute la composition « Bouton favori avec compteur » à 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-bouton-favori-compteur
 *
pattern.html
<!-- wp:html -->
<section class="bfc" data-bfc aria-labelledby="bfc-titre">
        <header class="bfc-head">
          <h2 class="bfc-title" id="bfc-titre">Recettes de saison</h2>
          <p class="bfc-total">Mes favoris : <strong data-bfc-total>0</strong></p>
        </header>
        <ul class="bfc-list">
        <li class="bfc-item">
          <span class="bfc-info"><strong class="bfc-name">Tarte Tatin aux pommes reinettes</strong><span class="bfc-meta">1 h 10 · facile</span></span>
          <span class="bfc-act">
            <button class="bfc-btn" type="button" aria-pressed="false" data-bfc-id="tatin" data-bfc-base="128" aria-label="Mettre en favori : Tarte Tatin aux pommes reinettes">
              <svg class="bfc-icon" viewBox="0 0 24 24" width="22" height="22" aria-hidden="true"><path d="M12 20.5s-7.5-4.6-9.3-9.2C1.4 7.9 3.6 4.5 7 4.5c2 0 3.5 1.1 5 2.9 1.5-1.8 3-2.9 5-2.9 3.4 0 5.6 3.4 4.3 6.8-1.8 4.6-9.3 9.2-9.3 9.2z"/></svg>
script.js
(() => {
  if (!document.querySelector(".bfc")) return;
  document.querySelectorAll("[data-bfc]").forEach((root) => {
    const on = (el, type, fn) => el.addEventListener(type, fn);
    const KEY = "forge-bfc-favoris";
    const buttons = [...root.querySelectorAll("[data-bfc-id]")];
    const status = root.querySelector("[data-bfc-status]");
    let saved = [];
    try {
      saved = JSON.parse(localStorage.getItem(KEY) ?? "[]");
    } catch {
      saved = [];
style.css
:root {
  --bfc-card: #ffffff;
  --bfc-text: #2a1f22;
  --bfc-muted: #5f5053;
  --bfc-line: #efe4e6;
  --bfc-heart: #be123c;
  --bfc-heart-bg: #fff1f3;
  --demo-bouton-favori-compteur-bg: #f7f1f2;
  --demo-bouton-favori-compteur-text: #2a1f22;
}

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

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

Buttons components in WordPress
  • A row of buttons to share the page on Facebook, LinkedIn, WhatsApp or by email, with a copy-link button and no third-party script.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A button that adds an event to Google Calendar, Outlook or any calendar through an .ics file, with no third-party service.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A button that copies an IBAN, a reference, an address or a promo code and confirms "Copied", on screen and to screen readers.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A WhatsApp button pinned to the bottom of the screen that opens a welcome bubble with common topics and a pre-filled message, with no third-party widget.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A bar pinned to the bottom of mobile screens with Call and Directions buttons and a live open-now status, built for trades and local shops.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • Circular icon buttons with an accessible tooltip on hover and focus.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component