How to execute a server side script when contact form 7 is submitted? [closed]

You need wpcf7_before_send_mail hook that gets triggered after sending email successfully.
Just add this in your functions.php.

add_action( 'wpcf7_before_send_mail', 'process_contact_form_data' );
function process_contact_form_data( $contact_data ){

    var_dump($contact_data->posted_data);
            $name = $contact_data->posted_data["your-name"];
    $email = $contact_data->posted_data["your-email"];

    echo $name ;
    echo $email;                
}

You can access fields by their name in $contact_data->posted_data array.


Yes. You can redirect to another page either with javascript or from above function. With javascript you need to add following line in Additional Settings of contact form you’ve created.

on_sent_ok: "location = 'http://youdomain.com/thankyou.php';"

But this won’t allow you to use values sent from form. So go for alternate method.

Redirect to thankyou page from above mentioned function. But before redirecting, set information you want to show on thankyou page into SESSION variables ( like name, email, subject fields from form ). Then on thankyou page, get those values and display them. Later you can destory session variables and check everytime if those certain variables are set. This will give control over hitting thankyou url directly from browser and show appropriate message saying page is not directly accessible.

Leave a Comment