In summary, I have a form I've built and I call a function in my plugin to set a cookie when the form is submitted and redirect to a page. Through testing I can see the cookie does get set when the form is submitting but when the redirect happens, I lose the cookie and the value associated with it. I've been crawling the forums and trying to debug for so long I think I'm missing something super simple. I've tried wp_redirect
, wp_safe_redirect
, header Location:
, window.open()
, etc.
If I use wp_die($_COOKIE)
before the redirect happens, I can see my cookie so I'm certain it happens after the page gets redirected to a different page.
How can I have it where the form is submitted, the cookie gets set and the redirect (to a different page on the same site) doesn't remove the cookie so I can work with the data on the redirected page?
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 is the form action:
<form action="' . esc_url(admin_url('admin-post.php')) . '" method="post" id="configurator">
Try this:
setcookie( 'antennasNow', json_encode($cookieValue), time()+3600, '/' );