I have a Gravity Forms page set up where, after you fill out the form, you are taken to a confirmation page where you can download some resources.
The problem with this setup is: Let’s say someone fills out the form and then gets to the confirmation page, and then leaves the site. Later they remember they need to download a different file from the confirmation page. They come back to the site and have to fill out the form again.
Is there some option in Gravity Forms to specify a cookie or something that indicates that the user already filled out the form and to take them directly to the confirmation page again?
Here’s some code on how you could simply leverage the gform_after_submission
hook of Gravity Forms to set a cookie based on your form ID once submitted, then check for that on the confirmation page by hooking into template_redirect
.
In order to customize the functionality, you’ll want to look at the docs for setcookie
, gform_after_submission
and template_redirect
.
For the form
// Make sure to swap out {your_form_id} with the ID of the form.
add_action( 'gform_after_submission_{your_form_id}', 'wpse_set_submitted_cookie', 10, 2 );
function wpse_set_submitted_cookie( $entry, $form ) {
// Set a third parameter to specify a cookie expiration time,
// otherwise it will last until the end of the current session.
setcookie( 'wpse_form_submitted', 'true' );
}
For the page
add_action( 'template_redirect', 'wpse_protect_confirmation_page' );
function wpse_protect_confirmation_page() {
if( is_page( 'my-confirmation-page' ) && ! isset( $_COOKIE['wpse_form_submitted'] ) ) {
wp_redirect( home_url( '/my-form/' ) );
exit();
}
}