wordpress 联系表格 7 - 在电子邮件发送中添加自定义功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11333183/
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-09-08 18:57:55  来源:igfitidea点击:

Contact Form 7 - add custom function on email send

wordpresswordpress-plugincontact-form-7

提问by Iladarsda

Just playing around with Wordpress / Contact Form 7.

Is it possible to add custom javascript function on successful email send event?

只是玩弄Wordpress / Contact Form 7

是否可以在成功的电子邮件发送事件上添加自定义 javascript 函数?

回答by Igor Jerosimi?

Write this in additional settings at the bottom of the contact form configuration page:

在联系表单配置页面底部的附加设置中写入:

on_sent_ok: "some js code here"

UPDATE: You can use it to call functions like this:

更新:您可以使用它来调用这样的函数:

on_sent_ok: "your_function();"

Or write some code (this one redirects to thank you page):

或者写一些代码(这个重定向到感谢页面):

on_sent_ok: "document.location='/thank-you-page/';"

回答by likesalmon

Contact Form 7 emits a number of Javascript events that bubble up to the document object. In version 4.1 they can be found in contact-form-7/includes/js/scripts.js. If you're using jQuery you can access those events like this:

Contact Form 7 发出许多冒泡到文档对象的 Javascript 事件。在 4.1 版中,它们可以在 contact-form-7/includes/js/scripts.js 中找到。如果您使用 jQuery,您可以像这样访问这些事件:

$(document).on('spam.wpcf7', function () {
    console.log('submit.wpcf7 was triggered!');
});

$(document).on('invalid.wpcf7', function () {
    console.log('invalid.wpcf7 was triggered!');
});

$(document).on('mailsent.wpcf7', function () {
    console.log('mailsent.wpcf7 was triggered!');
});


$(document).on('mailfailed.wpcf7', function () {
    console.log('mailfailed.wpcf7 was triggered!');
});

回答by Erez Lieberman

try this:

尝试这个:

$( document ).ajaxComplete(function( event,request, settings ) {
   if($('.sent').length > 0){
       console.log('sent');
   }else{
       console.log('didnt sent');
   }

});

回答by Clevelus

Example 1:

示例 1:

on_sent_ok: "location = 'http://mysite.com/thanks/';"

Example 2:In form script:

示例 2:在表单脚本中:

<div id="hidecform">
<p>You name<br />
    [text* your-name] </p>
...
</div>

Then at the bottom of the admin page under "Additional Settings" put the following:

然后在“其他设置”下的管理页面底部输入以下内容:

on_sent_ok: "document.getElementById('hidecform').style.display = 'none';"

回答by Chris Pink

Just a quick note that on_sent_ok is deprecated.

请注意, on_sent_ok 已被弃用。

Contact form 7 is now using event listeners, like this;

联系表单 7 现在正在使用事件侦听器,如下所示;

<script type="text/javascript">
  document.addEventListener( 'wpcf7mailsent', function( event ) {
      if ( '11875' == event.detail.contactFormId ) { // if you want to identify the form
       // do something
      }
  }, true );
</script>

Then add this to the wp_footer action.

然后将其添加到 wp_footer 操作中。

like this;

像这样;

add_action( 'wp_footer', 'wp1568dd4_wpcf7_on_sent' );

function wp1568dd4_wpcf7_on_sent() { 
  // the script above
}