5

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
  • Define "server side script"? Are you talking about PHP that can run in a WordPress context? A bash/Perl/other script? What? And what does the script need to do?
    – s_ha_dum
    Commented Aug 20, 2013 at 4:51

1 Answer 1

8

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.

6
  • Thanks for your reply @Phobos. Is it possible that when the form has been submitted then I will redirect to something like a thank you page wherein I could use the post variables from the form that was just submitted from contact 7?
    – drifter
    Commented Aug 20, 2013 at 12:58
  • @drifter please check updated answer. Commented Aug 20, 2013 at 15:39
  • 1
    this doesn't work. apart from not redirecting, the user gets no feedback from the form submission (despite the e-mail being sent) and is stuck with and endless loading animation. By the way, the authors officially discourage using the on_sent_ok hook (which actually works) and provide a solution that unfortunately doesn't work
    – zok
    Commented Aug 17, 2017 at 9:36
  • As it’s been long time author may have changed the api and hooks. Commented Aug 17, 2017 at 10:07
  • on_sent_ok is deprecated and not available
    – kanlukasz
    Commented May 18, 2020 at 17:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.