i'm struggling with a cookie matter for the past 2 hours. What i'm trying to do is to set a cookie with Javascript with a click event on a link and then let the default redirection to be happened. In the new page a want to perform an operation because of the cookies value and then delete it, so on a possible reload the site works normal again. The cookie will be a switch in order to determine if i'm going to run a pre_get_post function on my archive. The problem is that i cant find how to delete the cookie after i've grabbed it's value. Here is my code
function modify_query( $query ) {
$cookie_data = $GLOBALS['cookie_data'];
if ( $query->is_main_query() && isset( $cookie_data )) {
$meta_query = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'key',
'value' => $cookie_data,
)
)
);
$query->set( 'meta_query', $meta_query );
} // end if
}
add_action( 'pre_get_posts', 'modify_query' );
function get_cookie() {
if(isset($_COOKIE['test_data'])){
global $cookie_data;
$cookie_data = $_COOKIE['cookie_data'];
unset( $_COOKIE['cookie_data'] );
setcookie( 'cookie_data', '', time()-3600, '/');
}
}
add_action('init', 'get_cookie', 90);