Home  >  Q&A  >  body text

Cookies lost after WordPress redirect

Anyway, I built a form and called a function in the plugin to set the cookie when the form was submitted and redirected to the page. Through testing, I can see that the cookie is indeed set when the form is submitted, but when the redirect occurs, I lose the cookie and the value associated with it. I've been crawling forums and trying to debug this for a long time and I think I'm missing something super simple. I've tried wp_redirect, wp_safe_redirect, header Location:, window.open(), etc. p>

If I use wp_die($_COOKIE) before the redirect happens, I can see my cookie, so I'm sure it happens after the page redirects to some other page.

How can I submit a form, set a cookie, and redirect (to a different page on the same site) without deleting the cookie so that I can use the data on the redirected page? < /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');

If it helps, here are the form actions:

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

P粉198814372P粉198814372429 days ago659

reply all(1)I'll reply

  • P粉464088437

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

    Try this:

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

    reply
    0
  • Cancelreply