首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >联络表格7资料至Whatsapp Link

联络表格7资料至Whatsapp Link
EN

WordPress Development用户
提问于 2019-11-07 08:44:49
回答 1查看 4.2K关注 0票数 1

是否有可能从用户输入的联系人表格7到WhatsApp预填充的文本链接?

我试图让提交表单的人用预填的文本重定向到Whatsapp,作为表单输入的摘要。这就像通过WhatsApp确认订单一样。

我是一个网站开发的新手。我喜欢从社区中学到很多东西。非常感谢您的回复。

你好,阿菲克

EN

回答 1

WordPress Development用户

发布于 2019-11-08 10:13:36

要做到这一点并不容易。基本上,您需要存储您提交的数据,并使其在您的重定向页面上可用。

为此,您可以使用插件把我的CF7表格寄出去将提交的数据存储为post (可以在重定向完成后删除数据,或存储数据供以后参考),然后将以下内容添加到functions.php文件中,

代码语言:javascript
复制
add_filter('cf7_2_post_form_append_output', 'redirect_on_submit', 10, 3);
function redirect_on_submit($script, $attr, $nonce){
  //$attr cf7 shortcode attributes to check if this is the correct form.
  $url = site_url('/submitted'); //page slug to redirect to.
  $url = add_query_arg( array('cf72post' => $nonce,), $url);
  $url = esc_url($url);
  $script .= ''.PHP_EOL;
  $script .= 'document.addEventListener( "wpcf7mailsent", function( event ) {'.PHP_EOL;
  $script .= '  var save = document.getElementsByClassName("cf7_2_post_draft");'.PHP_EOL;
  $script .= '  if(save.length == 0  || "false" === save[0].value){'.PHP_EOL;
  $script .= '    location = "'.$url.'";'.PHP_EOL;
  $script .= '  }'.PHP_EOL;
  $script .= '}, false );'.PHP_EOL;
  $script .= ''.PHP_EOL;
  return $script;
}

这将基本上将您提交的表单重定向到在第一行中定义的页面URL (在本例中是'/ submitted‘),因此您可以将其设置为您想要的任何内容。

在提交的页面模板上,您现在可以使用以下函数访问保存的帖子,

代码语言:javascript
复制
if(isset($_GET['cf72post'])){
  $post_id = get_transient($_GET['cf72post']);
  $cf7Post = get_post($post_id);
  $whatsapp_text = $cf7Post->post_content;
  echo 'The following message will be sent to WhatsApp in 3 seconds:';
  echo ''.$whatsapp_text.'';
  echo '';
  echo 'window.onload=function(){ 
    window.setTimeout(function() { document.whatsappForm.submit(); }, 3000);
};';
}else if(isset($_POST['submitted-post-id'])){
  //the page is now being reloaded after 3 secs and you have the original data submitted data saved as the post_id.
  $post_id = $_POST['submitted-post-id'];
  $cf7Post = get_post($post_id);
  $whatsapp_text = $cf7Post->post_content;
  //send your $whatsapp_text to your whatsapp link
  // delete your $cf7Post if you don't need it.
}
票数 1
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/352086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档