我正在嘗試將我的頁面重定向到我從 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']
};
我需要將我的聯系表格重定向到 response['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
uj5u.com熱心網友回復:
CF7 插件沒有提供這樣的復雜性,解決這個問題的唯一方法是觸發雙重重定向,第一個使用插件頁面中記錄的 CF7 JavaScript 事件觸發重定向,第二個在服務器上使用 wp_redirect( )功能。
第 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 作為隱藏欄位添加到您的表單(您可以在提交時檢索)以及重定向 url 中,以識別正在重定向的提交。
第 2 步:使用'wpcf7_mail_sent'在答案中的鉤子上觸發的函式進行提交處理,并使用第 1 步中引入的唯一亂數將$response結果存盤在瞬態中,
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);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/316612.html
標籤:WordPress的 重定向 联系表格 7
