Cookies lost after WordPress redirect
P粉198814372
P粉198814372 2023-09-09 23:47:44
0
1
607

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 triedwp_redirect,wp_safe_redirect,header Location:,window.open(), etc. p>

If I usewp_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:

P粉198814372
P粉198814372

reply all (1)
P粉464088437

Try this:

setcookie( 'antennasNow', json_encode($cookieValue), time()+3600, '/' );
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!