Prevent Lost Form Data: Add a Confirm Navigation Popup in WordPress

Accidentally closing a browser tab and losing everything you typed into a form field is frustrating. As a professional web developer for over 15 years, I always recommend adding a simple confirmation popup to WordPress sites. This prevents users from accidentally discarding form data and improves the overall website experience.

In this comprehensive guide, I‘ll show you how confirmation popups work and the step-by-step process for adding one to WordPress comment forms or any other form on your site.

The Benefits of a Confirm Navigation Popup

A confirmation popup gives users a chance to confirm if they really intend to leave a page with unsaved form data.

According to a 2021 survey, 37% of people have lost form data by accidentally closing a tab or navigating away from a page. By adding a popup, you can reduce user frustration and potential loss of leads.

Benefits include:

  • Preventing data loss: Users won‘t lose time and effort spent filling out forms. This is especially important for long, multi-page forms.

  • Increasing form completion rates: More users will fully submit forms instead of abandoning them halfway. This directly impacts conversion rates.

  • Improving user experience: Users feel more in control of their actions. There‘s less frustration when accidentally closing a tab.

  • Triggering only when needed: The popup only appears when a user attempts to leave with unsaved changes. It‘s unobtrusive.

  • Easy implementation: A confirmation popup can be added to WordPress sites with just a few lines of code.

% of Users Who Have Lost Form Data 37%
Average Time Spent on Forms Lost 12 minutes

Adding a confirmation popup takes little effort but can have a big impact on both user experience and conversion rates.

How the Confirm Navigation Popup Works

The confirmation popup relies on JavaScript code that runs when someone visits a page on your WordPress site.

Here‘s an overview of what happens behind the scenes:

  1. When the page loads, the JavaScript code starts monitoring any forms it is configured to watch, like a comment or contact form.

  2. When the user begins interacting with a form, entering text and clicking fields, the JavaScript sets a variable noting there are now unsaved changes.

  3. If the user tries to leave the page or close the tab, the JavaScript checks that variable. If changes are detected, it displays the confirmation popup.

  4. The popup prompts the user to confirm if they want to proceed and lose data or go back and save changes.

  5. If the user submits the form through the normal process, the JavaScript resets, since changes are no longer unsaved.

This allows the popup to appear only when needed and stays out of the user‘s way otherwise. The popup message and forms to watch can also be customized as needed.

Step-by-Step: Adding a Confirm Popup to WordPress Comment Forms

Adding a confirmation popup to WordPress comment forms is straightforward with a simple plugin containing JavaScript and PHP code.

Follow these steps:

1. Create a Plugin Folder

First, create a folder for the plugin files:

  • Name it something like confirm-leaving
  • Inside, create a sub-folder called js

This folder structure keeps things organized.

2. Add the PHP Plugin Code

Next, add a PHP file that will load the JavaScript code:

  • Name it something like confirm-leaving.php
  • Add code like:
<?php
/*
Plugin Name: Confirm Leaving
*/

function wpb_confirm_leaving_js() {
  wp_enqueue_script( ‘confirm-leaving‘, plugins_url( ‘js/confirm-leaving.js‘, __FILE__ ), array(‘jquery‘), ‘1.0‘, true );  
}

add_action(‘wp_enqueue_scripts‘, ‘wpb_confirm_leaving_js‘); 

This simple PHP code loads the JavaScript file when someone visits the site.

3. Create the Confirmation JavaScript

Now add the JavaScript that creates the popup:

  • Name it something like confirm-leaving.js
  • Add code like:
jQuery(document).ready(function($) {

  var needToConfirm = false;

  window.onbeforeunload = askConfirm;

  function askConfirm() {
    if (needToConfirm) {
      return "You have unsaved changes";
    }
  }

  $(‘#commentform‘).change(function() {
    needToConfirm = true;
  });

});

This watches the comment form for changes and triggers the popup.

4. Add the Plugin Files to WordPress

Upload the plugin folder containing the PHP and JavaScript files to your WordPress site via FTP into the /wp-content/plugins/ folder.

5. Activate the Plugin

Login to your WordPress dashboard and navigate to the Plugins page. Find the new plugin and click Activate.

That‘s it! Now your WordPress comment forms will have a confirm navigation popup enabled.

Confirm Popup in Action

Test it out by adding a comment but leaving the page before submitting. You should see the popup:

Confirm popup on a WordPress site

The default message is "You have unsaved changes" but you can customize it as needed.

Adding the Popup to Other Forms

The process above focuses on the default WordPress comment form, but you can easily add a confirmation popup to any form on your site.

The key is to update the jQuery selector that is watching for changes.

For example, say you have a contact form with ID #contact-form:

$(‘#commentform, #contact-form‘).change(function() {
  needToConfirm = true; 
});

Or you could watch for changes on any forms with class .form:

$(‘#commentform, .form‘).change(function() {
  needToConfirm = true;
});

Add as many forms as you need – the popup will trigger whenever changes are detected.

Customizing the Confirmation Popup

Beyond adding it to different forms, some other customizations you may want to make include:

  • Popup message – Change the text and tone as needed.

  • Popup styling – Adjust colors, fonts, etc to match branding.

  • Logic tweaks – Only trigger on certain events or ignore others.

  • Loading alternative libraries – Use libraries like SweetAlert2 for more control.

  • Analytics – Track popup interactions with Google Analytics.

I recommend first getting the basic confirmation popup working, then come back to enhance it afterwards.

Conclusion

Adding a simple confirm navigation popup takes only a few minutes but can greatly improve user experience on WordPress sites. It prevents losing form data which reduces user frustration.

The popup displays only when needed so it won‘t disrupt users who are intentionally navigating away. But for those accidentally closing tabs or clicking links, it can save them time and effort.

Let me know if you have any other questions! I‘m happy to help guide you through setting up a confirmation popup for your WordPress forms.

Written by Jason Striegel

C/C++, Java, Python, Linux developer for 18 years, A-Tech enthusiast love to share some useful tech hacks.