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

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?

Topic session cookies functions php location-search Wordpress

Category Web


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), "/");

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.