WordPress 重定向後 Cookie 遺失
P粉198814372
P粉198814372 2023-09-09 23:47:44
0
1
573

總之,我建立了一個表單,並在提交表單並重定向到頁面時調用插件中的一個函數來設定 cookie。透過測試,我可以看到在提交表單時確實設定了 cookie,但是當發生重定向時,我丟失了 cookie 以及與其關聯的值。我一直在爬行論壇並嘗試調試很長時間,我認為我錯過了一些超級簡單的東西。我嘗試過wp_redirectwp_safe_redirectheader Location:window.open()等。 p>

如果我在重定向發生之前使用wp_die($_COOKIE),我可以看到我的 cookie,因此我確信它會在頁面重定向到其他頁面之後發生。

我怎麼能在提交表單、設定 cookie 且重定向(到同一網站上的不同頁面)時不刪除 cookie,以便我可以使用重定向頁面上的資料? < /p>

add_action('admin_post_action_configurator_form_submit', 'action_configurator_form_submit'); // logged in users add_action('admin_post_nopriv_action_configurator_form_submit', 'action_configurator_form_submit'); // not logged in users function action_configurator_form_submit () { // checking if nonce was submited and is valid, if not valid will exit check_ajax_referer('configurator_form_nonce', 'security'); if(isset($_POST['base_sku'])){ //store form entries as variables $base_Sku = $_POST['base_sku']; $type = $_POST['type']; $band = $_POST['band']; $polarization = $_POST['polarization']; $gain_Sku = $_POST['gain_sku']; $Az_Pattern = $_POST['azpattern']; $dual_Input = $_POST['dualinput']; $narrowband_Connector = $_POST['connector']; $beamtilt = $_POST['beamtilt']; $null_Fill = $_POST['nullfill']; $heavy_Duty = $_POST['heavyduty']; $invert_Mount = $_POST['invertmount']; $narrowband = $_POST['narrowband']; //Build sku group $antennaSku = $base_Sku . $type . $band . $polarization . $gain_Sku . $Az_Pattern; $fullSku = $antennaSku . '-' . $dual_Input . '-' . $narrowband . '-' . $narrowband_Connector . '-' . $beamtilt . '-' . $null_Fill . '-' . $heavy_Duty . '-' . $invert_Mount; $cookieValue = array( 'base_Sku' => $base_Sku, 'type' => $type, 'band' => $band, 'polarization' => $polarization, 'gain_Sku' => $gain_Sku, 'Az_Pattern' => $Az_Pattern, 'dual_Input' => $dual_Input, 'narrowband_Connector' => $narrowband_Connector, 'beamtilt' => $beamtilt, 'null_Fill' => $null_Fill, 'heavy_Duty' => $heavy_Duty, 'invert_Mount' => $invert_Mount, 'narrowband' => $narrowband, 'generatedSku' => $fullSku, ); //Find product by matching title based on Sku generated from form $page = get_page_by_post_name($antennaSku, OBJECT, 'product'); //Build cookie Usage: $data = json_decode($_COOKIE['antennasNow'], true); unset($_COOKIE['antennasNow']); setcookie('antennasNow', json_encode($cookieValue), time()+3600); if(!empty($page)){ //If matching product is found, redirect to it $url = get_permalink($page); redirect_to_antenna_product($url); } else { //Otherwise, redirect to a fallback page $url = '/selector-support'; redirect_to_antenna_product($url); } } } function redirect_to_antenna_product($url){ wp_safe_redirect($url); exit(); } function get_page_by_post_name($post_name, $output = OBJECT, $post_type = 'post' ){ global $wpdb; $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", $post_name, $post_type ) ); if ( $page ) return get_post( $page, $output ); return null; } add_action('init','get_page_by_post_name');

如果有幫助,這裡是表單操作:

P粉198814372
P粉198814372

全部回覆 (1)
P粉464088437

試試這個:

setcookie( 'antennasNow', json_encode($cookieValue), time()+3600, '/' );
    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板
    關於我們 免責聲明 Sitemap
    PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!