Allow user to select location and then set cookie for location in WordPress

Question

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(
         'loc'
        ,$_GET['loc']
        ,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
    exit;
}

}

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

0
, , , , Jessica Hawkins 7 months 0 Answers 83 views 0

Leave an answer