Dowemo
0 0 0 0


Question:

I have a site that detects a user's location and only displays posts that have the taxonomy with the matching city. If there is no match the user is redirected to a page to select available cities. This is my function:

function my_location($q){
if (!$q->is_main_query()) 
    return;
if ($q->is_search()) 
    return;
if ($q->is_archive()){  
    if ( ! is_admin())  {
        if ($userSlug!='Set'){
        $userInfo = geoip_detect_get_info_from_current_ip();
        switch ($userInfo->postal_code){
        case '86403':           
        case '86404':           
        case '86405': 
        case '86406':           
            $city="lake-havasu-city";           
            break;          
        case '86401':           
        case '86402':           
        case '86409':                       
            $city="kingman";            
            break;
        case '86429':           
        case '86430':           
        case '86439':
        case '86442':           
            $city="bullhead-city";          
            break;
        default:
            force_post_city($city);
            exit;
        }   
$q->set( 'tax_query', array(array('taxonomy' => 'pa_city','field' => 'slug',terms' => array( $city ),'operator' => 'IN'))); 
    }}
}
}
add_action( 'pre_get_posts', 'my_location' );

My question is, on the page that the user selects the city, how do I pass the city back to this function so they will pull the appropriate city? This is my form:

    <form method="post" action="new_location($term_taxonomy)">
<?php
function get_terms_dropdown($taxonomies, $args){
$myterms = get_terms($taxonomies, $args);
$optionname = "optionname";
$emptyvalue = "";
$output ="<select name='".$optionname."'><option selected='".$selected."'        value='".$emptyvalue."'>Select a City</option>'";
foreach($myterms as $term){
$term_taxonomy=$term->pa_city; //CHANGE ME
$term_slug=$term->slug;
$term_name =$term->name;
$link = $term_slug;
$output .="<option name='".$link."' value='".$link."'>".$term_name."</option>";
}
$output .="</select>";
return $output;
}
$taxonomies = array('pa_city'); // CHANGE ME
$args = array('order'=>'ASC','hide_empty'=>true);
echo get_terms_dropdown($taxonomies, $args);                
?>
<input type="submit" value="click" name="submit">
</form>

Any help would be much appreciated!


Best Answer:


The session suggestion was the key. I opted for a cookie instead. So, first I added a function that checks for the cookie on init of wordpress. If it does not exist, we try to determine their location and write the cookie. Then in the pre_get_post hook we direct them to either the page with the city deals or ff their city does not match an existing city, we direct them to the search city page:

    add_action( 'init', 'my_setcookie' );
    function my_setcookie() {
    if(!isset($_COOKIE['city'])) {
      $userInfo = geoip_detect_get_info_from_current_ip();
        switch ($userInfo->postal_code){
                case '86403':           
        case '86404':           
        case '86405': 
        case '86406':           
            $city="lake-havasu-city";           
            break;  
        case '86401':           
        case '86402':           
        case '86409':                       
            $city="kingman";            
            break;
        case '86429':           
        case '86430':           
        case '86439':
        case '86442':           
            $city="bullhead-city";          
            break;
        }   
    setcookie( 'city', $city, time() + 3600, COOKIEPATH, COOKIE_DOMAIN );
    }}
    function my_location( $q ){
if (!$q->is_main_query() ) 
    return;
if ($q->is_search()) 
    return;
if ($q->is_archive() ){ 
    if ( ! is_admin() ) {
        if ($userSlug!='Set'){
            if (empty($_COOKIE['city'] )) {
                echo "<script>window.location.href =        'http://thewebsite.com/select-city/';</script>";
                exit();
            }
        $city = isset( $_COOKIE['city'] ) ? $_COOKIE['city'] : 'not set';
        $q->set( 'tax_query', array(array(          'taxonomy'  => 'pa_city',           'field' => 'slug',          'terms' => array(  $city ),         'operator' => 'IN'      )));    
    }}
  }
    }
    add_action( 'pre_get_posts', 'my_location' );

So now we have them in the search city page. We pull the available cities from an attribute field and list them, upon click a javascript in the header is executed to update the cookie and send them to the deals:

            <form name="myCity" action="http://thewebsite.com/"  method="POST">
            <?php
                function get_terms_dropdown($taxonomies, $args){
                $myterms = get_terms($taxonomies, $args);
                $optionname = "optionname";
                $emptyvalue = "";
                $output ="<select name='".$optionname."'><option selected='".$selected."' value='".$emptyvalue."'>Select a City</option>'";
                foreach($myterms as $term){
                $term_taxonomy=$term->pa_city; //CHANGE ME
                $term_slug=$term->slug;
                $term_name =$term->name;
                $link = $term_slug;
                $output .="<option name='".$link."' value='".$link."'>".$term_name."</option>";
                }
                $output .="</select>";
                return $output;
                }
                $taxonomies = array('pa_city'); 
                $args = array('order'=>'ASC','hide_empty'=>true);
                echo get_terms_dropdown($taxonomies, $args);
                ?>
                <input type="submit" value="click" name="submit" onclick="WriteCookie()">
                </form>

Here is the javascript, we delete the existing cookie and set the new one:

                <script type="text/javascript">
                function WriteCookie()
                {
                    document.cookie = "city" + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
                    cookievalue = escape(document.myCity.optionname.value) + ";"
                    document.cookie='city='+cookievalue +'; expires=Fri, 3 Aug 2021 20:47:11 UTC; path=/';
                    window.location.href = "http://thewebsite.com"
                }

Works like a charm! Thank Halfer for the session suggestion.




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs