Trying to set a custom cookie on form submission [RESOLVED]

I’ve been asked to have content on a page be invisible until someone fills out a form and submits it. Upon submission, the page refreshes and content becomes visible.

I assume that means I need to create a cookie session, and then I can use the presence of that cookie variable to have the content show.

With that said, is there a way in Gravity Forms to create a cookie upon submission?

I did a search but only came up with a topic that had no answers and was closed.

Thank you kindly for any help! :slight_smile:

Gravity Wiz has a free solution that might address this:

However, if you would like to set a cookie when the form is submitted, you can use the gform_pre_submission filter:

The code to set the cookie might look something like this:

add_action( 'gform_pre_submission', 'set_custom_cookie_pre_submission') ;
function set_custom_cookie_pre_submission( $form ) {
    // Check the form ID if you want to target a specific form
    if ( $form['id'] == 1 ) {
        // Set the cookie name, value, and expiration time
        $cookie_name = 'my_custom_cookie';
        $cookie_value = 'some_value';
        $expiration = time() + ( 86400 * 30 ); // 30 days

        // Set the cookie
        setcookie( $cookie_name, $cookie_value, $expiration, '/' );
    }
}

Then, you would need to read the cookie before showing your content, whenever that needs to happen.

1 Like

Very much appreciate the reply back. I’ll take a look see at the suggestions. Thank you kindly!

1 Like

Minor update, finally got back to this, still testing, so far it looks like the plugin solution might do the trick!