Use 'on sent ok' action hook for two actions - Contact Form 7/WordPress

I need to use the on sent ok action hook for two actions 1) to track the email adress and 2) to send the user to a thank you page. I tried adding this into the 'Additional Settings' section on the Contact Form 7 panel but I am not sure if it works correctly. At least I got different results when using it with two different forms.

on_sent_ok: "fnTransaction('Contacted', 'userid=' + [your-email]);"

on_sent_ok: "location.replace('http://xxxxx.com/thank-you');"

Is it OK to use the action hook twice or can I combine this somehow? I'd appreciate your help!

Answers


couldn't you just call location.replace('http://xxxxx.com/thank-you'); inside the fnTransaction()-function?

edit:

write a new function that combines both:

on_sent_ok: "mySentOkFunction('Contacted', 'userid=' + [your-email]);"

function mySentOkFunction(param1, param2){
    fnTransaction(param1, param2);
    location.replace('http://xxxxx.com/thank-you');
}

I don't know Contact Form 7 but have you tried this:

on_sent_ok: "function(){ fnTransaction('Contacted', 'userid=' + [your-email]);location.replace('http://xxxxx.com/thank-you');}"

You can use:

on_sent_ok: "fnTransaction('Contacted', 'userid=' + [your-email]); location.replace('http://xxxxx.com/thank-you');"

Here location.replace didn't work, so I'm using:

location = 'http://xxxxx.com/thank-you';

This would be the final code:

on_sent_ok: "fnTransaction('Contacted', 'userid=' + [your-email]); location = 'http://xxxxx.com/thank-you';"

A clean approach is to use the hook wpcf7_contact_form_properties in a custom plugin, here is the plugin:

/*
Plugin Name: Multiple WPCF7's on_sent_ok
Plugin URI: http://kadimi.com/wpcf7-javascript-programmatically
Description: Use WPCF7's on_sent_ok many times.
Author: Nabil Kadimi
Version: 1.0
Author URI: http://kadimi.com/
*/

function se_21402617_wpcf7_properties( $properties, $contact_form_obj, $unused ){
    $properties[ 'additional_settings' ] .= 
        "\n"
        . 'on_sent_ok: "console.log(1);"' . "\n"
        . 'on_sent_ok: "console.log(2);"' . "\n"
        . 'on_sent_ok: "console.log(3);"' . "\n"
    ;
    return $properties;
}
add_filter( 'wpcf7_contact_form_properties', 'se_21402617_wpcf7_properties' , 10, 2 );

As you can see in the plugin code, I used on_sent_ok 3 times.

You can filter which form is affected by inspecting the $contact_form_object.

Source:

Code is derived from my blog post here.


Need Your Help

How to copy a row to another sheet based on if then

excel vba excel-vba

I'm trying to write a macro that will copy a row to another sheet if certain values are met. There are multiple possible destinations. This is what I've pieced together, but I'm sure it's messy.

Pager is not rendered on front page in Drupal 7

drupal drupal-7 drupal-theming

I see that there is a pager array inside the $page. But it doesn't print a pager.