Wordpress CF7 无法重定向邮件发送操作

Wordpress CF7 cannot redirect on mail sent action

我正在尝试将我的页面重定向到我从 curl post 响应中获得的 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

CF7 插件不提供这种复杂性,解决此问题的唯一方法是触发双重重定向,第一个使用 CF7 JavaScript 事件触发的重定向,如插件中所述 page, and the second one on the server using wp_redirect() function.

步骤 1,使用 CF7 重定向,这需要您在页面上加载表单时对 link 进行硬编码。为此创建一个默认页面,比如 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 使用步骤 1 中引入的唯一随机数,

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