Buddika Nishantha
Buddika Nishantha

Reputation: 53

How to access contact form 7 submitted data on a redirect page?

I'm using contact form 7 for survey. when i click contact form 7 submit button it's redirect to thank you page. I want to get all i submit data to the thank you page. How can i do it ?

Upvotes: 3

Views: 5320

Answers (1)

Aurovrata
Aurovrata

Reputation: 2279

there are 2 ways to achieve this,

1.Use a plugin which stores the submitted data such Post My CF7 Form (there is an FAQ on the plugin page which explains how to access the stored submission on the redirected page).

2.If you do no want to store and keep your submitted data, then you need to save the submitted data into a transient and access it from the redirected page, you can do this by,

Step 1: identify the current submission (this works even for non-logged in users.) using a WP nonce as a hidden field in your form, as well as place a js script to redirect to your page once a successful submission event is fired using an anonymous function hooked on the cf7 do_shortcode_tag display filter,

add_filter('wpcf7_form_hidden_fields','add_hidden_nonce');
function add_hidden_nonce($fields){
  $nonce = wp_create_nonce('cf7-redirect-id');
  $fields['redirect_nonce'] = $nonce;
  /*
   hook the shortcode tag filter using an anonymous function 
   in order to use the same nonce and place a redirect js event 
   script at the end of the form. 
  */
  add_filter('do_shortcode_tag', function($output, $tag, $attrs) use ($nonce){
    //check this is your form, assuming form id = 1, replace it with your id.
    if($tag != "contact-form-7" || $attrs['id']!= 1) return $output;
    $script = '<script>'.PHP_EOL;
    $script .= 'document.addEventListener( "wpcf7mailsent", function( event ){'.PHP_EOL;
    //add your redirect page url with the nonce as an attribute.
    $script .= '    location = "http://example.com/submitted/?cf7="'.$nonce.';'.PHP_EOL;
    $script .= '  }'.PHP_EOL;
    $script .= '</script>'.PHP_EOL;
    return $output.PHP_EOL.$script;
  },10,3);
  return $fields;
}

Step 2: hook the cf7 action 'wpcf7_mail_sent' once the submission has passed the validation/and email is sent.

add_action('wpcf7_mail_sent', 'save_posted_data_into_transient');
function save_posted_data_into_transient(){
  if(isset($_POST['redirect_nonce'])){ //save the data.
    //save the posted data from the form.  Note if you have submitted file fields you will also need to store the $_FILES array.
    set_transient('_cf7_data_'.$_POST['redirect_nonce'], $_POST, 5*60); //5 min expiration.
  }
}

Step 3: on your redirect page, you can now access your stored transient data, place this at the top of your page template,

<?php
if( isset($_GET['cf7']) ){
  $transient = '_cf7_data_'.$_GET['cf7'];
  $data = get_transient($transient);
  //$data['my-text-field']....
}
?>

Upvotes: 1

Related Questions