I need to create a region based url structure (not language based).

My idea is to add a query var with the region value to each request via the url, ie. a page would url would be: http://example.com/uk/sample-page, and the home url would be: http://example.com/uk/

I would then use this region query var as custom taxonomy on my custom post types, or as a “filter” on my page templates.

I’ve achieved this by manipulating the home_url:

// alter home url to match current region in the request uri
add_filter('option_home', function($home_url) {
    // extract region from current uri
    // valid regions: /usa /paris /uk
    preg_match('(/usa|/paris|/uk)', $_SERVER['REQUEST_URI'], $region);

    // if region is found, set it as a query var
    if (!empty($region[0])) {
        set_query_var('region', str_replace('/', '', $region[0]));

        // append region
        return $home_url . $region[0];
    } else {
        return $home_url;
    }
});

This actually works, but really feels like a hack and I don’t know how it affects plugins etc.

Is there another way to make this work?

Thanks

Read more here: Extend url to include region query var on each request


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: