首页  >  问答  >  正文

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粉198814372429 天前662

全部回复(1)我来回复

  • P粉464088437

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

    试试这个:

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

    回复
    0
  • 取消回复