Setting a custom cookie on form submission to store email address

Im trying to set a cookie up that has the value of the person email address from the gravity form they submit. I have created a global variable called $users_email and when the user submits the form I am capturing the value of the email field on the form and storing that in the variable.

I then have a set cookie function where I want to create the cookie and give it the value of the person name.

This is what I am currently working with but it doesnt seem to be setting the cookie

function divichild_enqueue_scripts() {
	wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}
add_action( 'wp_enqueue_scripts', 'divichild_enqueue_scripts' );


add_action( 'gform_after_submission_2', 'after_submission', 10, 2 );
function after_submission( $entry, $form ) {
	  global $users_email;		
	  $users_email = rgar( $entry, '7' );
	  return $users_email;
}

add_action( 'init', 'set_cookie' );
function set_cookie($users_email) {
$cookie_name = "my_visitor";
$cookie_value = $users_email;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");

}

Hi @user5b9a8430aeb527.8
You are trying to set a cookie after a Gravity Form submission. There are a few issues with the code you have provided:

  1. The set_cookie() function is hooked to the init action, which fires very early in the WordPress request lifecycle before the gform_after_submission_2 action is triggered. This means that the set_cookie() function will run before the after_submission() function has a chance to set the $users_email global variable.
  2. The set_cookie() function takes an argument, $users_email, but the function does not use this argument. Instead, it uses the global $users_email variable, which has not been set yet.

To fix these issues, you can try the following:

  1. Hook the set_cookie() function to the wp_footer action instead of the init action. This will ensure that the function runs after the form submission has been processed.
  2. Remove the argument from the set_cookie() function. Instead, use the global $users_email variable directly.

Here is the modified code:

function divichild_enqueue_scripts() {
	wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}

add_action( 'wp_enqueue_scripts', 'divichild_enqueue_scripts' );

add_action( 'gform_after_submission_2', 'after_submission', 10, 2 );

function after_submission( $entry, $form ) {
	  global $users_email;		
	  $users_email = rgar( $entry, '7' );
	  return $users_email;
}

add_action( 'wp_footer', 'set_cookie' );

function set_cookie() {
	global $users_email;
	$cookie_name = "my_visitor";
	$cookie_value = $users_email;
	setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
}

I hope this helps! Let me know if you have any questions.

Hi Faisal,

Thanks for your reply. What you have suggested makes sense, I have tried running this code though and am still unsuccessful.

It was suggested to me by someone else that running init would fire before the submission which is the same as your suggestion and makes sense. It was also suggested that the $user object may not be present when I try to set the cookie using the gform_after_submission hook.

So I can run the footer function in the footer like you suggested but I may still have the issue with the $user object not being available.

I am not sure the best way to test this in stages - ie I would want to run the first piece and verify its setting the $user_email value and then I can run the set cookie function in the footer.

You’re correct that the init action runs very early in the request, before the form submission is processed, and that the gform_after_submission_2 action runs later after the form has been submitted. Therefore, if you hook the set_cookie() function to the init action, it will run before the $users_email global variable has been set.

Regarding the availability of the $users_email global variable, you’re correct that this may be an issue. You can try one of the following solutions to see if it works:

One approach would be to hook the set_cookie() function to the wp action instead of the wp_footer. This action fires after the gform_after_submission_2 action, so by the time set_cookie() runs, the $users_email global variable should have been set.

add_action( 'wp', 'set_cookie' );

Another option would be to use the setcookie() function inside the after_submission() function. This way, you are setting the cookie immediately after the $users_email variable is set, and you don’t have to worry about the availability of the variable.

add_action( 'gform_after_submission_2', 'after_submission', 10, 2 );

function after_submission( $entry, $form ) {
	  $users_email = rgar( $entry, '7' );
    $cookie_name = "my_visitor";
    $cookie_value = $users_email;
    setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
}

You can test the setting of the $user_email variable by either putting the email value in a variable and echo out the variable, or you can use the browser developer tools to check the cookies.

Note that cookies are set on the client side, so you’ll need to test the code in a browser, not just in the code editor.

Let me know if you have any questions or need further help.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.