0

I have a problem in a custom function.

The function which adds to the WHERE clause in Store Locator Pro works correctly on its own, but I only want it to only work on a certain page, not on all my other Store Locator pages.

Should be simple, get the ID of the page I want it to work on and wrap the filter in a condition

function slp_country_filter($where){
    if( $_COOKIE["location_country"] != NULL ) {
      $SLPcountry = $_COOKIE["location_country"];
      $SLPcountryQuery = "AND (  sl_country = '" . $SLPcountry . "')";
    }
    else {
      $SLPcountryQuery = "AND (  sl_country = 'canada' or sl_country = 'usa' )";
    }   
    return $where . $SLPcountryQuery;
}

$id=get_the_ID();
if($id==613){
    add_filter( 'slp_ajaxsql_where' , 'slp_country_filter', 90 );
}

However this doesn't work, and after much investigation it seems that the variable $id is now NULL. Even though I can print it out in the header, the variable is reset to NULL by Store Locator Pro's call to admin-ajax.php. I determined this by changing the condition to if($id==NULL) and the conditional works.

I tried also to store the variable $id in a $_SESSION['SLP_ID'] variable and still the same thing happens. The moment the ajax call is made the variables are all returned as NULL and so the condition is false.

Appreciate any ideas on how to work around this.

Thanks.

  • 2
    an AJAX request is an entirely separate request from the page you are making the call from, so the fact that the id doesn't exist is not an error or unexpected behavior. That's how AJAX works, it's nothing to do with WordPress. – Milo Jan 16 '14 at 17:36
  • I get that, do you know how to get around that fact? – jimlongo Jan 16 '14 at 17:43
  • add the filter in your ajax callback before the query is run. – Milo Jan 16 '14 at 17:46
  • Solved this by storing the $id variable in a $_COOKIE, then just wrapping the filter in that condition if($_COOKIE['SLP_id']==613 ){ add_filter( 'slp_ajaxsql_where' , 'slp_country_filter', 90 ); } – jimlongo Jan 16 '14 at 20:37

Your Answer

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

Browse other questions tagged or ask your own question.