I am needing to allow a user to select a location from a dropdown on the front-end, that then sets a session cookie for that chosen location... any help here?

Ok so this is the outlined task: Location selector: 1st step is to simply have the location option slide down and then save the PHP cookie for later use

I am getting the cookie to set ' add_filter( 'query_vars', 'addnew_query_vars', 10, 1 ); function addnew_query_vars($vars) {
$vars[] = 'loc'; // loc is the name of variable you want to add
return $vars; }

add_action( 'send_headers', 'locations_redirect' ); function locations_redirect() { // Maybe even more elegant than simple $_GET, depending on if it was added: // $loc = get_query_arg( 'loc' );

if (
    isset( $_GET['loc'] )
    AND ! empty( $_GET['loc'] ) 
    empty( $_COOKIE['loc'] ) AND setcookie(
        ,time() +4 * WEEK_IN_SECONDS
    $path = user_trailingslashit( '/locations' );
    $scheme = is_ssl() ? 'https://' : 'http://';
    $url = is_multisite()
        ? network_site_url( $path, $scheme )
        : site_url( $path, $scheme );
    wp_redirect( $url ); // 302

} '

But I needing them be able to set that query var based on either a menu dropdown, or select menu?

  • Please elaborate your question. What have you done so far to achieve this? May 19, 2017 at 16:42

1 Answer 1


This code will set a cookie that essentially doesn't expire. Adjust the time value to make it expire sooner:

$cookiename = "ChocalateChip";
$cookievalue = "This is a tasty cookie value";
setcookie($cookienme, json_encode($cookievalue), time() + (86400 * 30*365*6), "/");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.