联系Form 7数据到WhatsApp链接

时间:2019-11-07 作者:Afiq Ali

是否可以从联系人表单7中的用户输入获取数据到WhatsApp预填充文本链接?

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

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

你好,Afiq

1 个回复
SO网友:Aurovrata

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

要实现这一点,您可以使用插件Post My CF7 Form 它允许您将提交的数据存储为帖子(可以在重定向完成后删除,或存储数据以供以后参考),然后将以下内容添加到您的函数中。php文件,

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 .= \'<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 .= \'</script>\'.PHP_EOL;
  return $script;
}
这基本上将您提交的表单重定向到第一行定义的页面URL(在本例中为“/已提交”),因此您可以将其设置为您想要的任何内容。

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

if(isset($_GET[\'cf72post\'])){
  $post_id = get_transient($_GET[\'cf72post\']);
  $cf7Post = get_post($post_id);
  $whatsapp_text = $cf7Post->post_content;
  echo \'<p>The following message will be sent to WhatsApp in 3 seconds:</p>\';
  echo \'<p>\'.$whatsapp_text.\'</p>\';
  echo \'<form name="whatsappForm" action=\'/submitted\'><input type="hidden" name="submitted-post-id" value="\'.$post_id.\'"/></form>\';
  echo \'<script type="text/javascript">window.onload=function(){ 
    window.setTimeout(function() { document.whatsappForm.submit(); }, 3000);
};</script>\';
}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.
}