Add a Notification Bar to WordPress Without a Plugin

A notification bar sits at the very top of your site and carries one short message: a sale deadline, a shipping notice, or a service update. This guide shows you how to add notification bar WordPress without plugin support, using the wp_body_open hook, a small dismiss script, and CSS you control.
The build stays light on purpose. There is no settings screen to learn and no extra plugin updates to chase. You get a bar that appears on every page, closes when a visitor dismisses it, and stops showing after a date you set.
What Is a Notification Bar and How Is It Different From a Sticky Footer Bar?
A notification bar, often called an announcement bar, appears above your header as the first thing a visitor sees. It usually holds one line of text with an optional link. Because it sits at the top, it works well for time sensitive news that should be noticed before reading begins.
A sticky footer bar plays a different role. It clings to the bottom of the screen while scrolling and usually pushes one action, such as a call button. That pattern suits mobile conversions but covers content and feels like an advertisement. Our guide to add a sticky footer bar in WordPress without a plugin covers that separate pattern. Choose the top bar when the message informs, and avoid running both at once.
When Should You Use a Notification Bar?
Use a notification bar for short lived, site wide facts. The message should make sense on any page, because the bar appears everywhere unless you limit it.
- Sale window: a discount with a clear end date and a link to the offer.
- Service notice: changed hours, delivery delays, or a planned outage.
- Event push: registration for a webinar, class, or local meetup.
- New resource: a fresh guide your regular readers should find fast.
Skip the bar when the message is permanent and already lives in your header or footer. A bar that never changes becomes wallpaper. Skip it also when the notice needs long explanation. Link to a full page instead, and keep the bar to one sentence with one clear destination.
How Do You Add the Bar With wp_body_open?
The wp_body_open hook fires right after the opening body tag, which makes it the natural home for a top bar. Your theme must call wp_body_open for the hook to work. Most modern themes do, since WordPress introduced the hook in version 5.2. If nothing appears later, this missing call is the first thing to check.
Add the code to a child theme functions.php file or a small custom plugin. A child theme protects your work from parent theme updates, and our guide to create a WordPress child theme shows the setup. Keep a backup before activating new code on a live site.
add_action('wp_body_open', 'vutg_notification_bar');
function vutg_notification_bar() {
$expiry = strtotime('2027-01-15 23:59:59');
if (current_time('timestamp') > $expiry) {
return;
}
?>
<div class="vutg-bar" id="vutg-bar" role="region" aria-label="Site announcement">
<p class="vutg-bar-text">
Winter sale ends soon.
<a href="https://example.com/sale/">View the offer</a>
</p>
<button class="vutg-bar-close" id="vutg-bar-close" type="button" aria-label="Dismiss announcement">×</button>
</div>
<?php
}
Replace the example link, message, and date with your own. The expiry check compares the site time with your deadline and returns early after that moment, so an old sale message cannot linger. Using current_time keeps the check aligned with the timezone in your WordPress settings. Keep the message short enough for one or two lines on a phone.
How Do You Add a Dismiss Button That Remembers the Choice for the Session?
A bar without a close button frustrates repeat visitors. This script hides the bar when the button is clicked and remembers that choice in sessionStorage for the current browser tab session. Session storage needs no cookie, holds no personal data, and clears when the tab closes, so the announcement returns on a fresh visit.
add_action('wp_footer', 'vutg_notification_bar_script');
function vutg_notification_bar_script() {
?>
<script>
(function () {
var bar = document.getElementById('vutg-bar');
var closeBtn = document.getElementById('vutg-bar-close');
if (!bar || !closeBtn) {
return;
}
try {
if (sessionStorage.getItem('vutgBarDismissed') === '1') {
bar.style.display = 'none';
return;
}
} catch (e) {}
closeBtn.addEventListener('click', function () {
bar.style.display = 'none';
try {
sessionStorage.setItem('vutgBarDismissed', '1');
} catch (e) {}
});
})();
</script>
<?php
}
The try and catch guards matter. Some browsers restrict storage in private modes, and an unguarded storage call can throw an error that stops the script. With the guard, the close button still hides the bar for the current page view. For a larger project, move the JavaScript into a theme file and load it with wp_enqueue_script. The WordPress developer documentation covers enqueue methods.
How Do You Style the Bar With CSS?
Style the bar so it reads as part of the site rather than an injected advertisement. Use your brand color for the background, keep text readable at a glance, and give the close button a generous tap target. Add the CSS to your child theme stylesheet or the Customizer additional CSS area. Our guide on how to add custom CSS in WordPress explains the safe routes.
.vutg-bar {
display: flex;
align-items: center;
justify-content: center;
gap: 12px;
padding: 10px 48px 10px 16px;
background: #0f3d5c;
color: #ffffff;
text-align: center;
position: relative;
font-size: 15px;
line-height: 1.4;
}
.vutg-bar-text { margin: 0; }
.vutg-bar a {
color: #ffd166;
font-weight: 600;
text-decoration: underline;
}
.vutg-bar-close {
position: absolute;
right: 8px;
top: 50%;
transform: translateY(-50%);
width: 36px;
height: 36px;
border: 0;
background: transparent;
color: #ffffff;
font-size: 22px;
line-height: 1;
cursor: pointer;
}
.vutg-bar-close:focus-visible {
outline: 2px solid #ffd166;
outline-offset: 2px;
}
@media (max-width: 600px) {
.vutg-bar { font-size: 14px; padding: 10px 44px 10px 12px; }
}
The right padding reserves space so text never slides under the close button, and the focus style keeps keyboard navigation visible. Change the two colors to match your palette, then check contrast before publishing. Decide whether the bar should stick during scrolling. A static bar scrolls away and suits announcements, while a sticky bar consumes screen height. If you make it sticky, test the WordPress admin bar offset for signed in users.
How Do You Add a Notification Bar in a Block Theme?
Block themes offer a no code route through the Site Editor. Open Appearance, then Editor, then Template Parts, and edit the header part. Add a Group block at the very top, set its background and text colors, and place a centered Paragraph block inside with your message and link. Save, and the bar appears wherever that header part is used.
The block route has two limits. A dismiss button needs a Custom HTML block holding the button markup and script, and date expiry needs the PHP check. Choose the PHP route when you need expiry, dismissal, or page targeting. Choose the block route for a simple static notice, and never run both at once.
What Should WooCommerce Stores Use Instead?
WooCommerce includes its own store notice feature, and stores should usually prefer it for shop announcements. Enable it in the Customizer under WooCommerce, then Store Notice, where you add the message and decide whether visitors can dismiss it. Running a custom bar and a store notice together stacks two announcements, so pick one. Use the custom bar only when you need a hard expiry date or placement rules the store notice cannot express.
How Do You Keep the Bar Accessible?
The markup in this guide uses a region role with an aria label so assistive technology can identify the announcement area. The close button carries an aria label because a multiplication symbol alone tells a screen reader user nothing about its purpose.
- Contrast: text and links must stand out clearly from the background.
- Button label: the close control needs a spoken name, not only a symbol.
- Keyboard path: the button and link must be reachable and visibly focused.
- Tap target: the close button needs enough size for thumbs.
Test with the keyboard only. Tab from the top of the page, confirm focus order and outline visibility, then zoom to 200 percent and confirm the text fits without clipping. These two checks catch most announcement bar problems in minutes.
How Do You Limit the Bar to Certain Pages?
Some notices belong on part of the site. A course launch notice may distract at checkout. Add a conditional check at the top of the function and return early where the bar should stay hidden.
if (is_checkout() || is_cart()) {
return;
}
Keep the rule list short and document the intent in a code comment. A bar governed by ten conditions becomes hard to predict, and a future editor may never work out why the notice vanished from one section.
How Do You Test the Bar Before and After Launch?
Test on staging first, then repeat the core checks live. View the homepage, a blog post, and a phone sized screen. Click the close button, reload in the same tab, and confirm the bar stays hidden. Open a new tab and confirm it returns.
- Display: the bar appears once at the top on desktop and mobile.
- Dismissal: closing hides it for the session, a new tab shows it again.
- Expiry: a past test date on staging hides the bar completely.
- Keyboard: focus reaches the link and button with a visible outline.
- Layout: the header keeps its spacing and the admin bar never overlaps.
If the bar shifts the layout, look at overall page weight before blaming the snippet. The bar itself is tiny, so jank usually comes from fonts or slow hosting. Our web hosting guide explains the plan features that keep pages responsive.
What Mistakes Break Notification Bars Most Often?
The most common failure is a theme that never calls wp_body_open. The code runs, no error appears, and the bar never prints. Other slips include a duplicate bar from two methods, an expiry date in the wrong year, and text wrapping into five lines on phones. Purge page cache after every change and retest signed out.
Frequently Asked Questions
Will the bar slow down my WordPress site?
A small bar with plain CSS and a short script adds negligible weight. Layout shift is the bigger risk, so reserve the bar space in your CSS and avoid heavy fonts or images inside the notice.
Can the bar stay hidden after a visitor closes it?
This guide hides it for the current tab session without cookies. For longer memory across visits, use localStorage or a cookie instead, and mention that storage in your privacy notice.
Why does my notification bar not appear?
Check three things in order: the theme calls wp_body_open, the expiry date sits in the future, and cache is cleared. A conditional guard or inactive snippet can also keep it hidden.
Should the bar be sticky while scrolling?
Keep it static for announcements so reading space stays clean. Use a sticky bar only when the message must remain visible, and test the admin bar offset plus mobile height first.
Last updated: December 2026.



