0

I am working on a classifieds website that has a custom taxonomy called 'countries'. In the main header menu I have these categories showing in a dropdown list of particular countries people can browse the site by.

How would I basically make it so when someone selects their country, it sets a session variable and or cookie with their location and from there on, every part of the site that shows a listing will use this value in a custom WP_Query to only query posts of that particular country?

So someone comes to the homepage, selects their country and then the page reloads with their chosen country for every page load thereafter.

2

You can definitly use PHP session variables. Follow this blog post ("Listing 3") for the best way to enable PHP sessions in WordPress. Namely, you need to use this code in your plugin or in your theme's funtions.php:

add_action( 'init', 'session_start', 0 );

After that you can use basic session variables to set the country when the user selects it, and subsequently access the country that the user has selected.

Good Luck!

  • I know PHP quite well but wasn't sure what approach to take. Because the country dropdown uses the Wordpress menu functionality, I wrote a proxy script that an AJAX request is made to and then the proxy script returns a success or failure and I do a window.location reload. Thanks for your help, it seems to work well/ – Dwayne Charrington Feb 11 '12 at 10:40
  • 1
    @GavinR I hope you don't mind the edit. Single functions can be attached to hooks the same as callbacks can be directly attached to for example native php functions: array_map( 'esc_attr', $array_to_validate );. – kaiser Feb 11 '12 at 11:00
  • 1
    @kaiser Good call! – Brian Fegter Feb 11 '12 at 11:05
  • 1
    @kaiser Thanks! Agreed - that's a simpler approach. – GavinR Feb 12 '12 at 17:47

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.