I have to develop a site using wordpress having thousands of pages. Pages will have the same content but there will be change in the location of the content. For example location will be Sydney, Melbourne etc. Is there any way that I can manage the virtual pages? The pages has to be indexed in the search engine as well. Can anyone suggest me to do it in effective way?
Thanks in advance
Theory
As WordPress doesn’t have some sort of geolocation detection script built in, we’ll have to make our own. As this script is javascript, we then need to get the resulting location data to php. Then we’re able to do a request or add something to our request.
I’ll now give you parts of code, some explanation and starting points, that will allow you to build a plugin (no need to build such logic into a theme).
Javascript & location
First we need a function, that is able to do the detection. There’re plenty of possibilities out there. You can see one possible solution below. Save it into a file, that you call
geo_detection.js
and save it into a new plugin folder, in a subdirectory named/js
. (You can go with any other solution as well).This script won’t be able to do a real detection if the user is not on a mobile device, as most providers deny the access, but it’s accurate enough to get the center of a city and find out the country in most cases.
Bringing the Device Data to PHP: Scripts we need
To further process the data, we need it available in php, as else we couldn’t make the DB request (or interact with one).
Gladly WP has a function that we can (miss)use in this case:
wp_localize_script();
.Bringing the Device Data to PHP: Processing the data
First, we want to process the data using another ajax file called
geo_ajax.js
that needs to get saved in our/js
folder as well. ThejQuery.ajax()
function below contains handlers/functions for all sorts or tasks: success, error and completion. We’ll only use success for now.Then we’re going to add our php AJAX request. This needs some safety checking as well.
Summed up
As you can see from the above, there’re tons of things to consider. What I showed â is just a starting point, as there’re dozens of routes that you can go and writing a complete plugin would more than just exceed the content of an answer. The good thing is: You now got a safe environment to start wrapping up your Database calls. But the actual solution depends on how you want to implement it.