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
The code in WordPress
<?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
*<!-- 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'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 -->(() => {
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();
});
});
})();: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.
Other toasts and alerts components in WordPress
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

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

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

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

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

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