I need to show a different phone number on the homepage based on the user’s ip location. It will be for a dynamic and changing number of store locations. So if a user visits the site and their ip location is within 50 miles of one of our stores, that store number will be displayed. If we do not have a store within 50 miles of their location, the default number will be displayed. When we open a new store, we will add the phone number and city (or zip, whatever is needed) into a custom field within WordPress. This data is what I need to use to do the geotargeting.
I have read countless articles and know I need to connect to a database like max mind, but I can’t figure out how to do the radius part. Can anyone point me in the right direction? A plugin or free service that can get me most of the way there?
Thank you.
A number of things are required.
Browsers will let the user(s) know that you are attempting to detect their location and give the user(s) an option to decline the geolocation attempt. Should they do so, you need to have a fallback such as a search page for your stores or display a particular store’s number instead e.g. the headquarters.
If the user(s) accept(s) the geolocation attempt, you could calculate their nearness based on a number of calculations, such as the one provided in step 3 of this tutorial. For working examples, please check out Bjorn’s store locator plugin which works flawlessly and comes with many UI options and a working radius detection visible in the search results of any of the demo pages. You can also dig into his code to see how he calculated the radius or which third party functions he used to do so.
If you are implementing this in wordpress, you might need to write a simple plugin for it and create a simple shortcode to use along with it or add your function to the functions.php file, although it is preferrable to write a plugin and keep your work organized and easier to edit/maintain later on as a result.
take a look at http://www.geoplugin.com/webservices/php
Output
PHP Class
This will output: