Kuki hilang selepas ubah hala WordPress
P粉198814372
P粉198814372 2023-09-09 23:47:44
0
1
713

Bagaimanapun, saya membina borang dan memanggil fungsi dalam pemalam untuk menetapkan kuki apabila borang diserahkan dan diubah hala ke halaman. Melalui ujian, saya dapat melihat bahawa kuki itu memang ditetapkan apabila borang diserahkan, tetapi apabila ubah hala berlaku, saya kehilangan kuki dan nilai yang dikaitkan dengannya. Saya telah merangkak forum dan cuba menyahpepijat ini untuk masa yang lama dan saya rasa saya kehilangan sesuatu yang sangat mudah. Saya telah mencuba wp_redirectwp_safe_redirectheader Location:window.open() dll. p>

Jika saya menggunakan wp_die($_COOKIE) sebelum ubah hala berlaku, saya dapat melihat kuki saya, jadi saya pasti ia berlaku selepas halaman mengubah hala ke beberapa halaman lain.

Bagaimanakah saya boleh menyerahkan borang, menetapkan kuki dan mengubah hala (ke halaman lain di tapak yang sama) tanpa memadamkan kuki supaya saya boleh menggunakan data pada halaman diubah hala? < /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');

Jika ia membantu, berikut adalah tindakan borang:

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

P粉198814372
P粉198814372

membalas semua(1)
P粉464088437

Cuba ini:

setcookie( 'antennasNow', json_encode($cookieValue), time()+3600, '/' );
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan