Set a Different Cookie Time on Form Submission

If you are not already familiar with using custom JavaScript, check out our  getting started guide.

Example

Let's imagine you have a form inside a popup. Whenever the popup is closed, it triggers a cookie

But, what happens if the form is submitted and the popup is never "technically" closed? The popup will continue to trigger and spam your users!

To make sure the popup doesn't appear again, we must set up a cookie that is created on form submit so that the user will never see the popup again.

Setup Your Cookie

First, set up the cookie where you're using the Manual JavaScript Cookie Creation Event in the Popup Editor.

After clicking Add, you will be able to customize every aspect of how your cookie will behave. For detailed explanations on all of the cookie settings and features, check out the  Cookie Settings and Features Doc.

Add and Edit the Manual JavaScript

Copy and paste the following code either in your parent or child theme  functions.php file, or use the My Custom Functions plugin.  

Replace the popup ID number in the code example with your popup's ID number. You can find the ID under Popup Maker > All Popups > CSS Classes (column). The number at the end of the class name 'popmake-'

/**
 *  Note 1: Replace the popup ID used below ('#popmake-123') with the
 *  value '#popmake-{integer} used with your popup.  From the WP Admin, refer
 *  to 'Popup Maker' -> 'All Popups' -> 'CSS Classes (column)' -> to find
 *  the popup ID.
 *
 *  Note 2: Use the cookie ID ('pum-{integer}' linked to the popup ID above
 *  for the code used on your site.
 *
 *  Add the following code to your theme (or child-theme)
 *  'functions.php' file starting with 'add_action()'.
 * -------------------------------------------------------------------------- */
add_action( 'wp_footer', 'my_custom_popup_scripts', 500 );
/**
 * Set a cookie to close a popup on form submit.
 *
 * @since 1.0.0
 *
 * @return void
 */

function my_custom_popup_scripts() { ?>
    <script type="text/javascript">
        (function ($, document, undefined) {

            jQuery('#popmake-123 form').on('submit', function () {
                jQuery.pm_cookie(
                    'pum-123', // The cookie name that is checked prior to auto opening.
                    true, // Setting a cookie value of true.
                    '10 years', // Plain english time frame.
                    '/' // Cookie path of '/' means site wide.
                );
            });

        }(jQuery, document))
    </script><?php
}

View the source on GitHub.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.