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

I am new to wordpress and I am trying to create a simple form. I am currently using contact form 7 and so far it find it nice. But now, I am trying to execute a server side code when the form is submitted. Basically, what I just want is to get the post variables from the from when it is submitted so that I can use the form data in the script. I still want the email functionality that sends to the email address when the form is submitted.

Is there a simple and more efficient way to do it without me need to change anything in the code?

1
1

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