WordPress Popup Without a Plugin: The Code and Its Limits
The HTML, CSS and JavaScript for a hand-made popup, what it cannot do, and when a plugin saves time.
Yes, you can add a popup to WordPress without a plugin. Modern browsers have a built-in popup element, <dialog>, and with about 40 lines of HTML, CSS and JavaScript you get a popup that opens after a delay or on a click, closes with Escape, and stays away for 30 days once closed. The full code is below.
We make a popup plugin, so you might expect us to talk you out of this. We will not. If you need one simple popup on a site you are comfortable editing, the code below is enough. The last section is an honest list of what it cannot do, so you can tell when a plugin starts to save you time.
Why use the <dialog> element
Most hand-made popup tutorials build the popup from a <div>, which looks right but misses what makes a popup usable for everyone: keyboard focus moves into it, Escape closes it, the rest of the page cannot be tabbed to while it is open, and screen readers announce it as a dialog. The <dialog> element opened with showModal() does all of that by itself, and it has worked in every major browser since 2022. It also draws its own backdrop, which you can style.
Step 1: The HTML
<dialog id="wd-offer" class="wd-offer" aria-labelledby="wd-offer-title">
<form method="dialog" class="wd-offer__close-form">
<button class="wd-offer__close" aria-label="Close">×</button>
</form>
<h2 id="wd-offer-title">One recipe every Friday</h2>
<p>Quick weeknight dinners, tested in a real kitchen.</p>
<a class="wd-offer__button" href="/newsletter/">Get the recipes</a>
</dialog>
The small form with method="dialog" is the standard way to make a close button for a dialog: submitting it closes the dialog, no JavaScript needed. The aria-labelledby tells screen readers that the heading is the dialog’s name.
Step 2: The CSS
.wd-offer {
width: calc(100% - 32px);
max-width: 420px;
border: 0;
border-radius: 12px;
padding: 36px 24px 24px;
box-shadow: 0 20px 60px rgba(0, 0, 0, .25);
}
.wd-offer::backdrop {
background: rgba(15, 23, 42, .55);
}
.wd-offer__close-form {
position: absolute;
top: 6px;
right: 6px;
margin: 0;
}
.wd-offer__close {
width: 44px;
height: 44px;
border: 0;
background: none;
font-size: 28px;
line-height: 1;
cursor: pointer;
}
.wd-offer__button {
display: inline-block;
padding: 12px 20px;
border-radius: 8px;
background: #7c3aed;
color: #fff;
text-decoration: none;
}
The close button is 44 pixels square so it is easy to tap on a phone, and the width rule keeps a 16-pixel margin on small screens.
Step 3: The JavaScript
(function () {
var dialog = document.getElementById('wd-offer');
if (!dialog || typeof dialog.showModal !== 'function') {
return;
}
var KEY = 'wd-offer-closed';
var DAYS = 30;
var DELAY = 10000; // 10 seconds
function closedRecently() {
try {
var at = Number(localStorage.getItem(KEY));
return at > 0 && Date.now() - at < DAYS * 86400000;
} catch (e) {
return false;
}
}
function open() {
if (!dialog.open) {
dialog.showModal();
}
}
// Remember when the visitor closed it (button, Escape or backdrop).
dialog.addEventListener('close', function () {
try {
localStorage.setItem(KEY, String(Date.now()));
} catch (e) {}
});
// A click on the backdrop closes the dialog.
dialog.addEventListener('click', function (event) {
if (event.target !== dialog) {
return;
}
var box = dialog.getBoundingClientRect();
var outside = event.clientX < box.left || event.clientX > box.right ||
event.clientY < box.top || event.clientY > box.bottom;
if (outside) {
dialog.close();
}
});
// Open by itself after a delay, unless closed in the last 30 days.
if (!closedRecently()) {
setTimeout(open, DELAY);
}
// Any element with the class "open-offer" opens it on click.
document.addEventListener('click', function (event) {
var opener = event.target.closest('.open-offer');
if (opener) {
event.preventDefault();
open();
}
});
})();
Two details are easy to get wrong. A click on the backdrop reaches the script as a click on the dialog itself, but so does a click on the dialog’s own padding, which is why the script compares the click position with the dialog’s box instead of closing on any click. And localStorage can throw in some private browsing modes, hence the try blocks: without them, one error would stop the popup working for those visitors.
We ran this exact code in current Chrome before publishing it and checked each behaviour: it opens after the delay with keyboard focus inside, a click on its padding leaves it open, Escape, the close button and a click on the backdrop each close it, a button with the class reopens it, and after closing it stays away on reload.
Step 4: Put it into WordPress
On one page
Edit the page, add a Custom HTML block, and paste the HTML, then the CSS inside <style>…</style>, then the JavaScript inside <script>…</script>. Only administrators (users with the unfiltered_html capability) can save scripts in a block; for anyone else WordPress strips them.
On every page
In a child theme’s functions.php, print it in the footer. This example also limits it to blog posts:
add_action( 'wp_footer', function () {
if ( ! is_singular( 'post' ) ) {
return;
}
?>
<!-- the dialog HTML, then <style> and <script> -->
<?php
} );
Use a child theme, not the theme itself, or the next theme update will erase it. A code snippets plugin also works, though at that point you are using a plugin after all.
To open it from a button, give the button the CSS class open-offer (in the block editor: Advanced → Additional CSS class(es)).
What this popup cannot do
Everything below is possible with more code. The question is whether you want to write and maintain it:
- Collect emails. The example links to a signup page. A form inside the popup needs your email service’s embed code or your own server code, plus spam protection.
- Edit without code. Changing the words means editing HTML. Fine for you, not for a colleague.
- Target pages without code. Every rule (“only on recipe posts, never on checkout”) is a PHP condition.
- Smarter frequency. It knows one thing: closed in the last 30 days. Not “shown three times already”, “once per visit” or “seen two pages”.
- More triggers. Scroll, exit intent and inactivity are each another block of JavaScript, with their own edge cases on touch screens.
- Measure anything. You will not know how many people saw it, closed it or clicked.
- Several popups. Each one is another copy of the code, and they do not know about each other.
A popup plugin is all of that already written and tested. The free Wing Popup plugin, for example, covers the editor, page targeting, frequency rules, forms with spam protection and Mailchimp, and ready-made designs, and loads its files only on pages where a popup will show. If your popup stays as simple as the one above, though, the code is a perfectly good answer.
Either way, the advice about timing and frequency is the same; see how often a popup should show.