I have some GEO IP code that directs users from a specific city, as interpreted by their IP address, to a specific URL. Rather than repeating the code below several times, as illustrated below for several cities. I'd like to have the code written in a more efficient way where I can list multiple cities in the argument.
The code is in the php section of my wordpress header
$geo = WPEngine\GeoIp::instance();
if ( $geo->city() == "New York") {
wp_redirect( '[login to view URL]', 301 );
exit;
}
if ( $geo->city() == "Oak Park") {
wp_redirect( '[login to view URL]', 301 );
exit;
}
As you can see, I have used the "if" statement twice for "New York" and "Oak Park." I'd like to be able to efficiently add 20 or 30 cities in the most efficient manner possible.
Thank you.
you have multiple solutions for this.
1. switch statement, this still will require additional modification if you add more cities,
[login to view URL] arrays and loops, this will allow you to perform essentially the same task for each city but with slightly different information: ie: $c = $geo->city(); foreach($cities as $city => $url){if($city === $c) {header('Location: '.$url)}}... I am not sure that it would really be this simple but my point is this is a relatively simple issue that can be handle with various methods we would have to talk to figure out which is truly the best for your situation...
3. (just throwing another one in) if you use the city name at the key to your array value you can do something like this $c = $geo->city(); $url = $cities[$c]; if($url) {wp_redirect($url);}else{wp_redirect('some fallback url');}exit;
On a different note... if you wanted to make this a bit more responsive you could use longitude and latitude which would allow you to redirect any user the closest "supported" city even if there geocode does not reflect that city (this would be very similar to the way craigslist autolocate feature works). and then if location services is off it can fallback to a city selection page. (this is much more extensive, not sure if you need something like that)
$25 USD in 1 day
1.0 (1 review)
1.3
1.3
10 freelancers are bidding on average $24 USD for this job
I think I am a good fit for this job because I have all the qualities that you are looking for. Experience is the golden factor in this project where you find in me at least 3+ years of working experiences over all the factors in your project.
I can improve this solution for you. Do you have a database in a backgroun where we can put a table which links the cities to states? Or does GeoIp::instance object contain that info? Should be easy task
I have a keen eye for attention to detail and high affinity to great design. The projects I make are fully functional, gorgeous looking and easily maintainable.