localStorage and sessionStorage in WordPress themes
Small preferences such as dark mode, a dismissed banner or the last opened tab do not need a user account or a cookie that travels to the server with every request. The Web Storage API keeps them in the browser: localStorage until the visitor clears it, sessionStorage until the tab is closed.
Problem: The dark mode switch and the closed announcement bar reset on every page, and on page load the light theme flashes before the dark one is applied.
Solution: Store the preferences in localStorage through a small wrapper with JSON and try/catch, apply dark mode with an inline script in the <head> before the page is painted, and use sessionStorage for state that should last only for the visit.
A safe wrapper
const haStore = {
get( key, fallback = null ) {
try {
const raw = localStorage.getItem( key );
return raw === null ? fallback : JSON.parse( raw );
} catch ( e ) {
return fallback;
}
},
set( key, value ) {
try {
localStorage.setItem( key, JSON.stringify( value ) );
} catch ( e ) {
// storage disabled, full, or private mode in an old browser
}
},
};
Storage holds only strings, so the values go through JSON.stringify() and back. Access can throw (blocked cookies and site data, a full quota), and the page should work anyway.
Dark mode without a flash
The class must be on <html> before the first paint, so this part cannot wait for a deferred script file:
add_action( 'wp_head', function () {
echo "<script>try{if(localStorage.getItem('haDark')==='true'){document.documentElement.classList.add('dark-mode');}}catch(e){}</script>\n";
}, 1 );
The toggle in the theme’s script:
const toggle = document.querySelector( '#ha-dark-toggle' );
if ( toggle ) {
toggle.setAttribute( 'aria-pressed', String( document.documentElement.classList.contains( 'dark-mode' ) ) );
toggle.addEventListener( 'click', () => {
const dark = document.documentElement.classList.toggle( 'dark-mode' );
haStore.set( 'haDark', dark );
toggle.setAttribute( 'aria-pressed', String( dark ) );
} );
}
A banner closed for seven days
const bar = document.querySelector( '.ha-announcement' );
if ( bar ) {
const closedAt = haStore.get( 'haBarClosed', 0 );
if ( Date.now() - closedAt < 7 * 24 * 60 * 60 * 1000 ) {
bar.hidden = true;
}
bar.querySelector( '.ha-announcement__close' )?.addEventListener( 'click', () => {
bar.hidden = true;
haStore.set( 'haBarClosed', Date.now() );
} );
}
Storage has no expiry, so the time is saved and compared. Change the key (haBarClosed2) when a new announcement should be shown to everybody again.
sessionStorage for the visit
sessionStorage.setItem( 'haActiveTab', 'reviews' ); const tab = sessionStorage.getItem( 'haActiveTab' );
Every tab has its own sessionStorage; it survives reloads but not closing the tab.
NOTE: Web Storage is readable by every script on the page, so never store tokens, nonces or personal data there. It is also invisible to PHP: if the server must know a preference (for example to render the dark theme without JavaScript), use a cookie instead; see cookie handling in WordPress.