2

I'm looking for some advice for the following scenario: We will have a site for a company with multiple locations throughout the US. Some of the pages are specific to a certain office location while other pages are generic for all locations.

I'm thinking of creating a page structure like this:

- Home (Generic)
- Locations
   - New York (This would be the New York office home page)
     - About US (Specific to the New York location)
     - Contact (Specific to the New York location)
   - Los Angeles
     - About US (Specific to the Los Angeles location)
     - Contact (Specific to the Los Angeles location)
- ... etc ...

When a user enters the website I want to guess their approximate location based on their IP address. Based on their location they should end up at the nearest office location home page, so for example /locations/new-york.

I'd want to set a session var when it doesn't exists yet containing the selected location so that I can redirect subsequent page requests to a sub page of that location.

My main question is how to handle the routing on subsequent requests. Say I have a session value and it contains the slug of the selected location:

$_SESSION['location'] = 'new-york'

The user is requesting the Contact page: http://example.com/contact What hook would I need to use to be able to rewrite that URL to http://example.com/locations/new-york/contact in case the session var is set?

3

SESSION variables aren't reliable, and won't work on some WP hosts. Even worse, you're going to prevent caching

But more importantly, don't route, redirect! Routing and redirecting are not the same thing

So, we want the following logic:

  • on the template_redirect action
    • if the user is on the homepage aka is_home
      • call wp_safe_redirect redirect to the closest office page
      • call exit

I'd want to set a session var when it doesn't exists yet containing the selected location so that I can redirect subsequent page requests to a sub page of that location.

But you have their IP, which you're using to figure out their location, why not just use that?

The user is requesting the Contact page: http://example.com/contact What hook would I need to use to be able to rewrite that URL to http://example.com/locations/new-york/contact in case the session var is set?

Be careful, when you say rewrite, it's not the same as redirecting. If I visit example.com/foo, and it gets rewritten to example.com/bar in PHP, it will still say /foo in my browser. You haven't moved me to /bar, you've changed what content displays at /foo. Redirect the user.

You might think I'm quibbling, but rewriting is a different thing, with a different set of APIs, that solves a completely different problem. The same with routing. Routing figures out given a URL which code should I load. Your problem is "Given a URL and a location, where should I redirect the user to"

So, on the contact page, before you output any HTML, test the user location and use wp_safe_redirect then exit

|improve this answer|||||
  • Thanks Tom. I appreciate your feedback. To answer your question "But you have their IP, which you're using to figure out their location, why not just use that?" Two reasons: I want the user to be able to manually select a location as well, so I'll have a dropdown list somewhere in the menu in case they disagree with the auto-selected location. Second reason is I prefer not having to repeat the same logic for every page request determining the ideal location. I understand the difference between routing and redirecting and should probably have phrased it differently. Thanks for the correction. – Ruben Mar 24 '18 at 23:24
  • If the user logs in you can use user meta. Else use cookies, but if its operating just on IPs then it's a fast check, and you only need to run it on pages that aren't location tied, you can assume somebody on the NYC office page is closest to the NYC office – Tom J Nowell Mar 24 '18 at 23:26

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.