最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

redirect - Contact Form 7 Data to Whatsapp Link

programmeradmin4浏览0评论

Is it possible to get the data from the user input in Contact Form 7 to a WhatsApp prefilled text link?

I'm trying to get the people who submitted the form to redirect to Whatsapp with the prefilled text as a summary of their input from the form. It's like an order confirmation via WhatsApp.

I am a newbie in website developing. I love to learn a lot from the community. Your kind reply is most appreciated.

Regards, Afiq

Is it possible to get the data from the user input in Contact Form 7 to a WhatsApp prefilled text link?

I'm trying to get the people who submitted the form to redirect to Whatsapp with the prefilled text as a summary of their input from the form. It's like an order confirmation via WhatsApp.

I am a newbie in website developing. I love to learn a lot from the community. Your kind reply is most appreciated.

Regards, Afiq

Share Improve this question asked Nov 7, 2019 at 8:44 Afiq AliAfiq Ali 111 silver badge3 bronze badges 1
  • Some info for you: Form Page > Thank You Page >Delay Redirect 3seconds to Whatsapp (I like to put the form input in the prefilled text here). The plugins I use are Contact Form 7 and Delay Redirect. – Afiq Ali Commented Nov 7, 2019 at 9:40
Add a comment  | 

1 Answer 1

Reset to default 1

This is not easy to achieve. Basically you need to store your submitted data and make it available on your redirected page.

TO achieve this you can use the plugin Post My CF7 Form which allows you to store the submitted data as a post (which can delete once the redirection is complete, or store the data for later reference) and then add the following to your functions.php file,

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;
}

this will basically redirect your submitted form to your page URL defined on the first line (in this example '/submitted', so you can set that to whatever you want.

On the submitted page template, you can now access the saved post with the following function,

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.
}
发布评论

评论列表(0)

  1. 暂无评论