WordPress Contact Form 7 – Intelligent Thank you pages

I have been developing a site that required the “thank you” page after the user submits a form to be aware of the options submitted. “Easy!” you say, “just grab the data from the $_POST or $_GET array”.

Unfortunately this isn’t really an option in CF7, as the data is emailed out (or stored in the database depending how you have it set up) and then the user is shown a confirmation message. I used this method to get the user redirected to a thank you page, but by that point all the data from the form is no longer available.

My solution: grab the data as soon as it is submitted and store it in the user’s session. To do this I added an action (in my theme’s functions.php) to get fired when the ‘wpcf7_before_send_mail’ event happens, and store it in the database, like so:

function set_sessions($cf7){
$_SESSION['formdata'] = $cf7->posted_data['menu-848'];
return false;
}

add_action( ‘wpcf7_before_send_mail’, ‘set_sessions’ );

‘Menu-848′ is just the one piece of data I wanted to grab. If you want to store the whole set of data, just use $_SESSION['formdata'] = $cf7->posted_data; instead.

Once it’s stored, you just have to get it out of the session array wherever you want.

Hope this helps, let me know if you have any questions.

Edit: Just a note in case it’s needed – you should also ensure that you actually have a session open, as WordPress doesn’t do this automatically.

To do this, add the following to your wp_config file:


/**
* Enable sessions
*/
if (!session_id())
session_start();