I need some help. I'm trying to create a Confirm Navigation Popup on WordPress. I want the popup to appear when the user tries exiting a form page without submitting the form.

Screenshot of the popup I'm talking about: https://ibb.co/3CvqgKH

I used the Forminator form plugin to create the form in a blank page. The form consists of several required questions so it only lets the user submit the form if all of the required fields are filled in. When all of the required fields are filled in, the form accepts the submission and redirects the user to a different URL (A thank you page).

When a user enters this form page, I want a Confirm Navigation popup to appear every time the user tries exiting the page. However, the popup should not appear if the user exits the page by submitting the form.

I have found this article: https://www.greengeeks.com/tutorials/article/incomplete-form-confirmation-in-wordpress/

I followed the guide to create a custom plugin and it kind of works.

Here's what I did:

1. Created a folder on my computer, called confirm-leaving-form.

2. Added a PHP file, called confirm-leaving.php, inside the confirm-leaving-form folder:

* Confirm Leaving
* Plugin Name: Confirm Leaving Form
* Plugin URI: http://www.mydomainexample.com
* Description: This plugin shows a warning to users when try they forget to hit submit button on a comment form.
* Version: 1.0.0
* Author: My name
* Author URI: http://www.mydomainexample.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt

function wpb_confirm_leaving_js() {

wp_enqueue_script( 'Confirm Leaving Form', plugins_url( 'js/confirm-leaving.js', __FILE__ ), array('jquery'), '1.0.0', true );
add_action('wp_enqueue_scripts', 'wpb_confirm_leaving_js');

3. Created another folder, called js, inside the confirm-leaving-form folder.

4. Added a JavaScript file, called confirm-leaving.js, inside the new js folder:

jQuery(document).ready(function($) {

$(document).ready(function() {
needToConfirm = false;
window.onbeforeunload = askConfirm;

function askConfirm() {
if (needToConfirm) {
// Put your custom message here
return "Leaving so soon? Your data will be lost if you continue.";

$("#forminator-module-2959").change(function() {
needToConfirm = true;


forminator-module-2959 is the ID of my form.

5. Uploaded the whole confirm-leaving-form folder to the "/plugins/" directory of my website (used the FileZilla FTP application).

6. Activated the new plugin on WordPress.

This created a custom plugin which almost does what I want, but not quite.

Here are the issues:

1. The popup only appears if the user has started filling out the form. If the form is empty and the user tries exiting the page, the popup does not appear.

2. The popup appears when the user has filled out the form and hits the “Submit” button. The popup should not appear if the user is trying to submit it.

Ideally, the popup should appear even if the form is empty but not appear if the user exits the form by clicking the "Submit" button.

Is this possible?

1 Answer 1


First things first, change the name of the functions - you have them the same as the tutorial - as a rule, always make them something unique just to reduce the possibility of conflicts. So instead of wpb_confirm_leaving() you should maybe change it to something like jkkr_confirm_leaving. Just make sure you change it everywhere. But really that's just me being extra cautious and pedantic. Now, to address the issues you're having... They're javascript issues...

jQuery( document ).ready( function( $ ) {
    $( document ).ready( function() {
        /* You have it set as 'needToConfirm = false when the document loads. */
        //needToConfirm = false;
        /* Let's make it true instead. */
        needToConfirm = true;
        window.onbeforeunload = askConfirm;
    } );
    function askConfirm() {
        if( needToConfirm ) {
            /* Put your custom message here. */
            return 'Leaving so soon? Your data will be lost if you continue.';
    /* Next, you actually have the above functions executing when you change the form,
       or more correctly being set to true, when you change the form.
       So instead, let's change the status of needToConfirm to FALSE if the user is 
       clicking submit, because you're already validating the form using 
       Forminator with required fields and all that, so, assuming they have 
       filled everything out correctly and they click submit, we want to just
       let them happily leave the page. 
       Quick note, if you can get the id="" of the submit button, 
       you can swap that out below instead of my ambigious guess. */
    $( '#forminator-module-2959 input[type="submit"]' ).on( 'click', function() {
        needToConfirm = false;
    } );
} );

You can also just remove all of my comments but the basic gist of it is as follows...

  1. When the page with the form loads we're going to immediately set the needToConfirm to TRUE.
  2. Now that the needToConfirm is TRUE as soon as the page loads, we're going to leave it as such until someone clicks the input[type=submit] for this specific form, at which point, since the validation for the form is now handled by Forminator, we're going to set the needToConfirm to FALSE because it's no longer required, the user has filled out the form and we don't want to pester them.

The way the tutorial works is based on only setting the the message up to be displayed if the user has bothered to actually interact with the form.


So a couple of things, I just realized now that we were wrapping a document.ready inside of another document.ready, that was redundant.

Your first issue is because the script appears on every page, so it'll execute on every page because we've set the needToConfirm variable as true from the outset. I have remedied that by first checking if the form exists before doing anything else. if( $( '#forminator-module-2959' ).length > 0 )

For the second issue, well that's actually the browser's doing:

Note: To combat unwanted pop-ups, some browsers don't display prompts created in beforeunload event handlers unless the page has been interacted with. Moreover, some don't display them at all.

Read the spec here: https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload

The third issue, in my original snippet I wrote:

Quick note, if you can get the id="" of the submit button, you can swap that out below instead of my ambigious guess.

I was genuinely guessing and it turns out that I was targeting a non-existent element. Now the key here is that clicking SUBMIT is what disables the message. But since there is no input[type="submit"] the message never got disabled. Looking at your code I found that $( '#forminator-module-2959 #submit button' ) is the correct element to target. So I've swapped that out in the code and now, clicking it, should disable the message when exiting the page.

jQuery( document ).ready( function( $ ) {
    /* First, lets restrict this to pages that actually have the form... */
    if( $( '#forminator-module-2959' ).length > 0 ) {   
            // Set the need to confirm to true
            var needToConfirm = true;
            window.onbeforeunload = askConfirm;
            function askConfirm() {
                if( needToConfirm ) {
                    /* Put your custom message here. */
                    return 'Leaving so soon? Your data will be lost if you continue.';
            $( '#forminator-module-2959 #submit button' ).on( 'click', function() {
                needToConfirm = false;
            } );
} );

This should get you much closer to what you're after.

  • Hi Tony. Thank you for your response! I implemented the changes and it does not work as it should. Here are the issues: 1. It now shows the popup sitewide, not just the form page. 2. The popup only gets triggered if you click somewhere in the website before trying to exit. It does not appear if you exit immediately without interacting with the page. 3. The popup still appears when I submit the form and get redirected to a different URL. Do you have an idea what might be causing these issues?
    – JOKKER
    Jan 4, 2021 at 19:05
  • I edited my original question to include the URL of my form page at the end in case you need to check the form for yourself.
    – JOKKER
    Jan 4, 2021 at 19:11
  • I can take a look in a bit. I already know the root cause of the first issue and it’s easily remedied. Jan 4, 2021 at 19:42
  • 1
    That sounds like a caching problem; so the server (depending on where you're hosted) and your browser 'remember' the files they serve and attempt to load them from memory rather than always fully re-parsing the files on every page load. This is done in an attempt to improve user experience by speeding things up. Your best bet, while developing and actively working on a site is to disable the server caching and to also change the version number of your plugin and the associated files. In your enqueueing for scripts you know how there's a version number towards the end? Change that. Jan 5, 2021 at 12:25
  • 1
    For example, you probably have something like this: wp_register_script( 'confirm-script', plugins_url( 'confirm-script.js', __DIR__), array( 'jquery' ), '1.0', true );. Every time you make a change, ensure that the '1.0' gets bumped a bit. So before you upload change it to: wp_register_script( 'confirm-script', plugins_url( 'confirm-script.js', __DIR__), array( 'jquery' ), '1.1', true ); and so on. Jan 5, 2021 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.