Skip to content

Dismissible announcement bar in WordPress

An announcement bar at the top of the page with a link and a button to close it.

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

alerte-bandeau-annonce.php
<?php
/**
 * Plugin Name: Forge, Bandeau d'annonce refermable
 * Description: Une barre d'annonce en haut de page avec un lien et un bouton pour la fermer. Ajoute la composition « Bandeau d'annonce refermable » à 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-alerte-bandeau-annonce
 *
pattern.html
<!-- wp:html -->
<div class="announce" role="region" aria-label="Annonce" data-announcement>
        <p><strong>Nouveau :</strong> la facturation électronique est disponible pour tous les comptes. <a href="#facture-electronique">Découvrir</a></p>
        <button type="button" aria-label="Fermer l&#x27;annonce" data-announcement-close><svg aria-hidden="true" viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round"><path d="M6 6l12 12M18 6 6 18"/></svg></button>
      </div>
<!-- /wp:html -->

<!-- wp:group {"className": "page-alerte-bandeau-annonce", "layout": {"type": "default"}} -->
<div class="wp-block-group page-alerte-bandeau-annonce"><!-- wp:heading {"level": 1} -->
<h1 class="wp-block-heading">Tableau de bord</h1>
<!-- /wp:heading -->
<!-- wp:paragraph -->
script.js
(() => {
  if (!document.querySelector(".announce")) return;
  document.querySelectorAll("[data-announcement]").forEach((bar) => {
    bar.querySelector("[data-announcement-close]").addEventListener("click", () => {
      bar.hidden = true;
      // Keep keyboard users in the page instead of losing focus with the removed button.
      const main = document.querySelector("main");
      main.setAttribute("tabindex", "-1");
      main.focus();
    });
  });
})();
style.css
:root {
  --an-bg: #4c1d95;
  --an-text: #f5f3ff;
  --an-ring: #fde68a;
  --demo-alerte-bandeau-annonce-bg: #faf8ff;
  --demo-alerte-bandeau-annonce-text: #1e1145;
}

@media (prefers-color-scheme: dark) {
  :root.forge-follows-system:not([data-theme="light"]) {
    --an-bg: #2e1065;
    --an-text: #f5f3ff;

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

Toasts and alerts components in WordPress
  • A banner that warns visitors when they lose the network and announces when it is back, using browser events, with no request.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A bell with an unread badge that opens a notification panel: icons by type, read and unread items told apart, and a "Mark all as read" button.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • The four classic interface messages: information, success, warning and error.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • Three callouts for technical documentation: note, tip and warning.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A notice warning of a scheduled service interruption, with date, time and impact.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A dashed callout highlighting an offer and its code, with the end date.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component