Set the form field in CF7 actions and pass it to the landing page
-
I have hidden input in my form:
[hidden my-field]
My goal is to have this field set (some data from external API) after the user clicks submit. I also want to be able to access this variable on the “Thank you page” that the user will be redirected to after submitting the form.
I have added an action:
“Actions -> Submission Actions -> type: Redirect”
Select a page: Thank you page
with these settings switched to ON:
– Redirect as X-WWW-FORM-URLENCODED (Form Post)
– Pass all the fields from the form as URL query parameters
This is the code of the “Thank you page” I’m redirected to:<?php echo $_POST['my-field']; ?>
If I set the my-field in the wpcf7_posted_data action then everything works fine and I can see the variable on the “Thank you page”:
add_action('wpcf7_posted_data', 'my_fun', 10, 1); function my_fun($form_data) { $form_data['my-field'] = $_POST['my-field'] = 'some value'; return $form_data; }
The problem is that this action is called before validation. After validation, these two actions are called:
add_action('wpcf7_before_send_mail', 'my_fun', 10, 3); function my_fun($cf7, $abort, $submission) { $_POST['my-field'] = 'some value'; return $cf7; }
add_action('wpcf7_submit', 'my_fun', 10, 3); function my_fun($cf7, $result) { $_POST['my-field'] = 'some value'; }
…but then on the “Thank you page” I can’t access the $_POST[‘my-field’] variable. Why your plugin, when redirecting to the landing page, does not pass the fields set in the wpcf7_before_send_mail and wpcf7_submit actions? How can I achieve it?
- The topic ‘Set the form field in CF7 actions and pass it to the landing page’ is closed to new replies.