Skip to content

Display Text Confirmation Before Redirecting in Gravity Forms

Chris Eggleston 2 min read

Symptoms

Out-of-the-box Gravity Forms does not support using two confirmations at the same time when a form is submitted.

However, because we were recently asked how to show a confirmation message before redirecting the user to another URL, we decided to put together a couple of options.

How To Fix It

There are (at least) three different ways of accomplishing this.

  1. You can use the gform_confirmation filter to output a confirmation message and then perform a redirect (code snippet below).
  2. Add custom JavaScript to the text confirmation (code snippet below).
  3. Use a third-party redirect plugin.

1. The gform_confirmation filter:

Copy and paste the code snippet below into your themes functions.php file or use a functionality plugin like WPCodeBox (recommended).

Once the code is added you will need to make three modifications:

  1. On line 31 add your form ID number(s), replacing 5, 16, 43 with your form ID(s).
  2. Edit the confirmation message on line 40, change it to whatever you’d like, or leave it as is.
  3. On line 42 set the number of seconds you’d like to delay the redirect. The snippet is currently configured for a two second delay, 2000 = 2 seconds.
gf-dual-confirmation.phpGist by chriseggGitHub
<?php
// Copy everything below this line

/**
* GravityRanger Dual Confirmation (text + redirect)
* https://gravityranger.com/display-text-confirmation-before-redirecting-gravity-forms
*
* Uses the gform_confirmation hook to display a text confirmation message before triggering the redirect confirmation.
* This code is a modified version of the snippet provided on docs.gravityfroms.com
* 
* @instructions:
* 
*  1. Add this code snippet to your functions.php file or a functionality plugin like WPCodeBox
*  2. Enter your form ID number(s) on line 31
*  3. Edit the confirmation message on line 40
*  4. Set how many seconds you want the redirect to be delaied on line 42 (2000 = 2 seconds)
* NOTE: This only works if your default confirmation is configured as a redirect
*
* 
* @version  0.1
* @author   GravityRanger
* @license  GPL-2.0+
* @link     https://gravityranger.com/
* @credits  https://docs.gravityforms.com/gform_confirmation/
*
*/

add_filter( 'gform_confirmation', function ( $confirmation, $form, $entry, $ajax ) {
    GFCommon::log_debug( __METHOD__ . '(): running.' );
 
    $forms = array( 5, 16, 43 ); // i.e Targets forms with id 5, 16 and 43.
 
    if ( ! in_array( $form['id'], $forms ) ) {
        return $confirmation;
    }
 
    if ( isset( $confirmation['redirect'] ) ) {
       $url          = esc_url_raw( $confirmation['redirect'] );
        GFCommon::log_debug( __METHOD__ . '(): Redirect to URL: ' . $url );
        $confirmation = 'Your message has been received loud and clear. Please standby...';
        $confirmation .= "<script type=\"text/javascript\">
	setTimeout(function() { window.open('$url')}, 2000); </script>";
    }
 
    return $confirmation;
}, 10, 4 );

Configure the Redirect Confirmation

For this snippet to work, your form’s default confirmation must be set to redirect and configured with the desired URL.

  1. Go to the form Settings
  2. Click Confirmations
  3. Click to edit the Default Confirmation
  4. Click Redirect
  5. Enter your URL
  6. Click Save Confirmation

2. Custom JavaScript text confirmation

This option requires that the default confirmation be configured to use the Text type and the snippet below be added to the editor text tab.

  1. Go to the form Settings
  2. Click Confirmations
  3. Edit the Default Confirmation
  4. Make sure Text is selected
  5. Click on the Text tab
  1. Copy and Paste the snippet below into the editor
  2. Make any changes you’d like to the message
  3. Set your desired number of seconds to delay the redirect (i.e 2000 = 2 seconds)
  4. Check to Disable auto-formatting
  5. Click Save Confirmation
gf-text-redirect-confirmaton.jsGist by chriseggGitHub
/**
* GravityRanger
* https://gravityranger.com/display-text-confirmation-before-redirecting-gravity-forms/
*
* Used to display a text message after a form is submitted, but triggers a redirect 2 seconds after message is displayed.
* 
* Instructions: Copy and paste lines 11-17 into a Gravity Forms text confirmaton. Make any necessary changes to the text or number of seconds, i.e 2000 = 2 seconds, (line 16).
*
*/

Thank you for your submission. Please stand by, something exciting is about to happen...

<script type="text/javascript">
	setTimeout(function() {
	   window.location = 'https://yourdomain.com/thank-you/';
	}, 2000); // 2 seconds
</script>

Redirect plugin

You will need to install the Shortcode Redirect plugin. This is essentially the same solution as using the custom JS above, it is just a code-free method if you prefer to avoid messing with code.

  1. Go to the form Settings
  2. Click Confirmations
  3. Edit the Default Confirmation
  4. Make sure Text is selected
  5. Create your message
  6. Add the plugin shortcode to the message (below)
[redirect url='https://yoururl.com' sec='2']
  1. Add your redirect URL to the shortcode
  2. Set the number of seconds you want to delay the redirect
  3. Click Save Confirmation

Need More Help?

If you still need help, use the chat or leave a comment below and we’ll get back to you.

Comments

  1. Hugatron

    Thank you! After much searching, this is the first where I got clear instructions on how to accomplish. With other sites (including gravity forms) I had no idea where to place the code.

    I didn’t even think about adding js to the confirmation directly, using the ‘text’ tab.

    1. Chris Eggleston

      Thanks for the feedback! I’m glad the article was able to help you get things working!

  2. Charlie

    This is great guys. Quick question concerning the first solution…does it still work with multiple redirect confirmations setup to trigger on specific pages?

    1. Chris Eggleston

      Hey Bryan,

      It does not appear to be working anymore. I will see if I can come up with a fix.

      It does still work!

        1. Chris Eggleston

          Hey Bryan, if you can’t get that snippet to work in the confirmation, you can try this solution:

          #1 Setup your text confirmation
          #2 Use a code snippet plugin or use this doc to help you identify where to add this JS
          #3 Make suer ajax is enabled on the form embed
          #4 Modify the code (ares tagged in the code) and implement it

          jQuery(document).ready(function(){ jQuery(document).on('gform_confirmation_loaded',
          function(event, formId){ if(formId == 1)// change 1 with your forms ID
          {
          function Redirect() { window.location = "https://google.com/";//put redirect URL here
          }
          setTimeout(function() {
          Redirect();
          }, 2000);// 2 seconds
          }
          });
          })

          1. Bryan

            All right. Thank you. Unfortunately, since I am not the SuperAdmin, I don’t have the capability of adding a plugin, but I can discuss it with him and see what he want to do.

          2. Bryan

            I do believe so. Are you seeing it working if auto-formatting is disabled?

          3. Chris Eggleston

            What version of PHP and Gravity Forms are you running? You can check this by going to Forms > System Status

          4. Bryan

            Figured out the issue. Script works fine. Because I am not the SuperAdmin, however, Gravity Forms would not permit me to include Javascript in the confirmation. My SuperAdmin was able to do it for me, however. Thanks for all your help with this.

Ask a question or leave a comment

Your email is never shown. For help with a specific form, share what you expected and what happened instead.