搜尋

首頁  >  問答  >  主體

WordPress 重定向後 Cookie 遺失

總之,我建立了一個表單,並在提交表單並重定向到頁面時調用插件中的一個函數來設定 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');

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

<form action="' . esc_url(admin_url('admin-post.php')) . '" method="post" id="configurator">

P粉198814372P粉198814372442 天前682

全部回覆(1)我來回復

  • P粉464088437

    P粉4640884372023-09-10 17:03:22

    試試這個:

    setcookie( 'antennasNow', json_encode($cookieValue), time()+3600, '/' );
    

    回覆
    0
  • 取消回覆