Sorry if this has been asked - I haven't figured out how to search it in a way that leads me where I want to go.

We have a client who wants a site built that represents four geographical areas and so the first URL part after the bare domain should represent that like so: example.com/state-code/

Let's say:

Member A signs up and indicates FL as their home State (using a custom field select dropdown).

Member B signs up and indicates AZ as their home State.

So the site looks like: http://www.example.com/fl/ to User A, and http://www.example.com/az/ to User B.

We want to use the same core for the site, same template files, same database, etc. We just want to be able to make some basic conditional changes in those templates based on location as a geo-relevant site is important to end users but all other functionality doesn't need to be duplicated because it would be redundant. E.g.

/* header.php */

if ($user->location === 'fl') {
  echo '<img src="logo-florida.png" />';
} else if ($user->location === 'az') {
  echo '<img src="logo-arizona.png" />';
// etc.

Should this just be done by assigning a Category or Tag to our Users (and therefore something like %category% in the Permalink) and matching that with the custom field entry? Or is there a way to dynamically prepend the State to the URL depending on the variable contents from the custom State field in the User?

How can we easily and cleanly add a custom value into our URL/permalink structure?

1 Answer 1


If I understand you well, I think you need to find a string like {fl or az} in the current URL and this can be done by exploding the current URL into pieces.

If you confused about the code, we assigned and checked variables like {$url_path and $url_parts} inside if statement

 * get_site_url() function return the 
 * current site url without trailing slash 
 * like { **http://www.example.com/fl** } 
 * FUN: parse_url: with const {PHP_URL_PATH} return the URL path after {.com}
 * FUN: explode: separate words by delimiter '/' and {@} to avoid errors
 * because of parse_url return the path after {.com} so the string 
 * {$url_path} starts with {/fl} so you we used the key number {1} not {0}

$site_url = get_site_url();

if( ! empty( $url_path = parse_url( $site_url, PHP_URL_PATH ) ) ) {

    if( is_array( $url_parts = @explode( '/', $url_path ) ) && 
        ( count( $url_parts ) > 0 ) &&
        array_key_exists( 1, $url_parts ) ) {

        switch( $url_parts[1] ) {

            case 'fl': {

                    /* === DO === */

                } break;

            case 'az': {

                    /* === DO === */

                } break;



  • Sorry, no, you've misunderstood. We need to prepend this info to the url, not search for it. Sep 12, 2018 at 2:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.