是否有可能从用户输入的联系人表格7到WhatsApp预填充的文本链接?
我试图让提交表单的人用预填的文本重定向到Whatsapp,作为表单输入的摘要。这就像通过WhatsApp确认订单一样。
我是一个网站开发的新手。我喜欢从社区中学到很多东西。非常感谢您的回复。
你好,阿菲克
发布于 2019-11-08 10:13:36
要做到这一点并不容易。基本上,您需要存储您提交的数据,并使其在您的重定向页面上可用。
为此,您可以使用插件把我的CF7表格寄出去将提交的数据存储为post (可以在重定向完成后删除数据,或存储数据供以后参考),然后将以下内容添加到functions.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 .= ''.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‘),因此您可以将其设置为您想要的任何内容。
在提交的页面模板上,您现在可以使用以下函数访问保存的帖子,
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.
}https://wordpress.stackexchange.com/questions/352086
复制相似问题