javascript 对两个动作使用“on sent ok”动作钩子 - 联系表格 7/WordPress

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21402617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:50:55  来源:igfitidea点击:

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

javascriptwordpresscontact-form-7

提问by Charles Ingalls

I need to use the on sent okaction 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操作钩子执行两个操作:1) 跟踪电子邮件地址和 2) 将用户发送到感谢页面。我尝试将其添加到 Contact Form 7 面板上的“其他设置”部分,但我不确定它是否正常工作。至少在将它与两种不同的形式一起使用时,我得到了不同的结果。

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!

两次使用动作挂钩是否可以,或者我可以以某种方式组合它吗?我很感激你的帮助!

回答by hildende

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

你不能location.replace('http://xxxxx.com/thank-you');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');
}

回答by alexP

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

我不知道联系表格 7,但你有没有试过这个:

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

回答by Daniel Kossmann

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.replace 不起作用,所以我使用:

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';"

回答by Nabil Kadimi

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

一个干净的方法是wpcf7_contact_form_properties在自定义插件中使用钩子,这是插件:

/*
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_ok3 times.

正如您在插件代码中看到的那样,我使用了on_sent_ok3 次。

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

您可以通过检查$contact_form_object.

Source:

来源:

Code is derived from my blog post here.

代码来自我的博客文章here