Three rules
- A dismissed bar never paints. The decision is made in the page head, before anything is drawn.
- The server decides which bar shows. The schedule — and in the full edition the page, the cart, the role and the cutoff — is settled in PHP. The browser is never sent bars it would not show.
- A bar is text somebody typed, and it is treated that way. The message is filtered at save and at render, the button address is checked, and a colour must be a colour.
Choosing the bar
On each page the plugin goes through the bars in the order you listed them and takes the first one (full edition: up to At most this many on one page) that:
- is Switched on,
- is inside its dates — today, in your site's timezone, is on or after Starts and on or before Ends, and
- Free edition: carries no conditions. Full edition: has every condition true.
The choice is made once per request and reused.
Printing it
The bar is plain HTML printed by the server: on wp_body_open for a top bar, and Full edition: in the footer for a bottom bar. It works before any script has run, and a page cache can store and serve it.
Its colours are written as two CSS custom properties on the bar itself (--gmab-bg, --gmab-fg), rather than as a stylesheet in every page's head.
The stylesheet and the small close script are only loaded on a page that has a bar.
Why a dismissed bar does not flash
A cache serves the same page to everybody, but "this visitor closed the bar" is a fact about one person. Most bar plugins therefore print the bar and remove it after the page has loaded — so somebody who closed it last week sees it appear and vanish on every page.
This plugin decides earlier:
- Pressing × stores a cookie for that bar,
gmab_closed_<id>, and removes the bar. - On every later page, a short script in the
<head>reads those cookies (and the older sharedgmab_closedlist, so a dismissal made before the per-bar cookies still holds) and, for each id, adds a classgmab-closed-<id>to the<html>element and a one-line style hiding[data-gmab-id="<id>"]. - That runs before the body exists, so the bar is hidden before it is drawn.
The page's HTML stays identical for everybody, which is what keeps it cacheable. The snippet contains no values from your settings at all.
Each bar has its own id, and no two bars share one, so closing the January sale never closes the shipping-deadline bar. Editing a bar keeps its id, so a visitor who closed it does not see it again because you fixed a typo.
Stays closed for (days) sets the cookie's lifetime when the visitor closes the bar; 0 makes it a browser-session cookie. Each bar has its own cookie, so each keeps its own lifetime: a bar that stays closed for a day and one that stays closed for a month do not overwrite each other.
The filter rule
The leading plugin in this category has had several security issues, all of the same shape: a bar's message is trusted because an administrator typed it, and then an importer, a restored backup or a less-trusted role writes it instead. So:
- the message passes
wp_kses_post()when saved and when shown — links and basic formatting survive, scripts do not; - the button address goes through
esc_url_raw(), sojavascript:never becomes a link; - a colour is checked with
sanitize_hex_color(); anything else (#fff;}</style><script>is the classic trick) becomes the default colour; - the screen requires
manage_woocommerce.
The free edition's checks are the same as the full edition's.
Conditions — Full edition
Every condition must be true; adding one narrows who sees the bar and never widens it. A condition no code understands counts as false, which hides the bar — something you notice on your own screen, rather than a bar your customers see everywhere.
Pages are recognised with WooCommerce's own functions (checkout, cart, account, product, category, shop, home). Cart value is the cart subtotal, before tax and discounts, and money is read the way people type it. It is before this time today is judged on the store's clock; a time that cannot be read matches nothing.
Worked example — Full edition
Two bars, in this order:
- "Order by 3pm for same-day dispatch", conditions It is before this time today: 15:00 and Everywhere except these pages: checkout.
- "Free delivery over £40", condition The cart is worth at most: 39.99.
At 10:00 a visitor with £25 in the cart sees both (with At most this many on one page at 2). At 16:00 they see only the second; at the checkout, only the second; with £45 in the cart before 3pm, only the first.
In the free edition, both bars carry conditions, so neither is shown.
