【问题标题】:Wordpress CF7 cannot redirect on mail sent actionWordpress CF7 无法重定向邮件发送操作
【发布时间】:2021-11-21 01:29:56
【问题描述】:

我正在尝试将我的页面重定向到我从 curl 发布响应中获得的 url。
这是我的代码:

add_action( 'wpcf7_mail_sent', 'action_wpcf7_mail_sent'); 
function action_wpcf7_mail_sent( $contact_form ) {
    if ( $contact_form->id !== 127 )
        return;
    $wpcf7 = WPCF7_ContactForm::get_current();
    $submission = WPCF7_Submission::get_instance();
    $save = $submission->get_posted_data();
    $data = //the data I send
    $url= 'http://my-url';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']);
    $response = json_decode(curl_exec($ch));
    curl_close ($ch);
    //I need to redirect my contact form to response['redirect_url']
}; 

我需要将我的联系表单重定向到响应['redirect_url']
我尝试了多种方法,但都不起作用:
wp_redirect($response["redirect_url"]);
header( 'Location: '.$response["redirect_url"] );
两者都使用exit;

?><script type="text/javascript">
        location = <?php echo $response["redirect_url"] ?>;
</script><?php

但是他们都给出了错误。 如果您有任何想法,请告诉我!

编辑
我试着稍微改变一下我的代码!
我试图从 js 中捕获 cf7mailsent,然后像这样调用 php 函数:

document.addEventListener( 'wpcf7mailsent', function( event ) {
  var inputs = event.detail.inputs;
  $.ajax({
    type: "POST",
    datatype:"json",
    url:jsforwp_globals_sub.ajax_url,
    data: { 
        action:"send_this_data",
        ajax_nonce:jsforwp_globals.send_info,
        info:inputs,
    }
  }).done(function(mess) {
    msg=jQuery.parseJSON(mess);
    if(typeof msg['redirect_url']!="undefined"){
        window.location.href = msg['redirect_url'];
    }else if(typeof msg['message']!="undefined"){
        alert(msg['message']);
    }else{
        alert('failed'+mess);
    }
  }).fail(function(xhr, status, error) {
  });
});
add_action("wp_ajax_nopriv_send_this_account","action_wpcf7_mail_sent");
function action_wpcf7_mail_sent() {
    $save=$_POST["inputs"];
    //the rest of the code from $save = $submission->get_posted_data();
    echo json_encode($response);
    wp_die();
}

但它不起作用,什么是正确的解决方案?
非常感谢!

更多信息
问题是当我尝试使用 $save 时,我很确定它会给出 null

【问题讨论】:

  • 这是否为您提供了更好的方法? - contactform7.com/redirecting-to-another-url-after-submissions
  • 不幸的是,我从functions.php中检索了需要将用户重定向到的url,所以我不知道如何在那里传递url
  • 两件事。 1:你应该使用这个与你正在做的$contact_form-&gt;id() 来获取表单的ID。 2. 您需要将 ajax 操作附加到 wpcf7mailsent DOM 对象以获取 URL。要么禁用 JS 提交,要么不使用 ajax/wp-json API 表单提交。
  • 谢谢!我一直在尝试。我在将联系表单的值传递给 php 函数时遇到问题,有什么线索吗?我将更改问题以显示新代码。
  • 您的操作需要send_this_data,但您的wp_ajax_nopriv_send_this_account 使用send_this_account - 我还会为登录用户添加wp_ajax_{action}

标签: wordpress redirect contact-form-7


【解决方案1】:

CF7 插件不提供如此复杂的功能,解决此问题的唯一方法是触发双重重定向,第一个使用 CF7 JavaScript 事件触发重定向,如插件page 中所述,第二个在服务器使用 wp_redirect() function.

第 1 步,使用 CF7 重定向,这需要您在页面上加载表单时对链接进行硬编码。为此创建一个默认页面,例如form-redirection

add_filter('wpcf7_form_hidden_fields','add_hidden_nonce');
function add_hidden_nonce($fields){
  $nonce = wp_create_nonce('cf7-redirect-id');
  $fields['redirect_nonce'] = $nonce;
  /*
   hook the shortcode tag filter using an anonymous function 
   in order to use the same nonce and place a redirect js event 
   script at the end of the form. 
  */
  add_filter('do_shortcode_tag', function($output, $tag, $attrs) use ($nonce){
    //check this is your form, assuming form id = 1, replace it with your id.
    if($tag != "contact-form-7" || $attrs['id']!= 127) return $output;
    $script = '<script>'.PHP_EOL;
    $script .= 'document.addEventListener( "wpcf7mailsent", function( event ){'.PHP_EOL;
    //add your redirect page url with the nonce as an attribute.
    $script .= '    location = "'.esc_url(home_url('/form-redirection/?cf7='.$nonce)).'";'.PHP_EOL;
    $script .= '  }'.PHP_EOL;
    $script .= '</script>'.PHP_EOL;
    return $output.PHP_EOL.$script;
  },10,3);
  return $fields;
}

注意:引入唯一的nonce 可确保您可以唯一地识别提交。 nonce 被添加为表单中的隐藏字段(您可以在提交时检索)以及重定向 url,以识别正在重定向的提交。

第 2 步:使用在答案中的 'wpcf7_mail_sent' 钩子上触发的函数进行提交处理,并将生成的 $response 结果存储在 transient 中第一步,

add_action( 'wpcf7_mail_sent', 'action_wpcf7_mail_sent'); 
function action_wpcf7_mail_sent( $contact_form ) {
    if ( $contact_form->id !== 127 ) return;
    //make sure the nonce is available too,
    if(!isset($_POST['redirect_nonce']) return;
    ...
    $response = json_decode(curl_exec($ch));
    curl_close ($ch);
    //I need to redirect my contact form to response['redirect_url']
    //so store in a transient
    set_transient('_cf7_data_'.$_POST['redirect_nonce'], $response['redirect_url'], 5*60); //5 min expiration.
}

第 3 步:在您的重定向页面上,检测表单提交,从瞬态中获取存储的响应数据,然后重定向到您的最终位置...

add_action('init', 'redirect_cf7_submission');
function redirect_cf7_submission(){
  if( isset($_GET['cf7']) ){
    $transient = '_cf7_data_'.$_GET['cf7'];
    $url = get_transient($transient);
    if(false === $url) $url = home_url(); //or back to the form page?
  }else $url = home_url(); //or back to the form page?
  wp_redirect($url);
}

【讨论】:

  • 非常感谢您的详细解释!我尝试了类似的东西,但我想我会尝试将代码更改为你的,它看起来更高效!
  • 你是 wc,请告诉我它是否有效,因为我没有时间完全测试它。如果代码不起作用,最好更正代码,以便其他人也可以使用它。
猜你喜欢
  • 2021-02-01
  • 1970-01-01
  • 2022-09-29
  • 1970-01-01
  • 2021-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-14
相关资源
最近更新 更多