• Resolved Bill Kellaway

    (@bkellaway)


    I have a site that uses wildcard subdomains. Enter www. or wwg. or whateve and the site works – only on the first visit. If a user visits a second page they are taken to the www version because of the URL in the menu’s displaying the absolute path that is built using the get_site_url().

    I am trying to add a filter to over ride the built in get_site_url() function. The filter to the last line of the functions file.

    Also for this to work you need wildcard subdomains set up – which I have.

    Any ideas why this isnt working ?

    function set_site_subdomain() {

    $server_name = $_SERVER[‘SERVER_NAME’] ;
    $host = explode(‘.’, $server_name );
    $this_subdomain = $host[0];

    $site_url_array = explode(“.”, get_site_url() ) ;

    $new_url = “https://”.$this_subdomain.”.”.$site_url_array[1].”.”.$site_url_array[2] ;

    return $new_url ;

    }

    add_filter(‘get_site_url’, ‘set_site_subdomain’, 1 );

    Thanks in advance

Viewing 1 replies (of 1 total)
  • Thread Starter Bill Kellaway

    (@bkellaway)

    Okay I figured this out. My whole approach was flawed. I was assuming by overriding the get_site_url() this domain would be passed to the menus. Wrong. The solution was to change the menu directly. So instead of using the set_site_subdomain() function I created a new function that updated the menu links directly.

    Here is the codeL

    /*		Keep the Subdomain the same in the menus */
    
    function change_menu($items){
    
    $server_name = $_SERVER['SERVER_NAME'] ;
    $host = explode('.', $server_name );
    $this_subdomain = $host[0];
    
    $site_url_array = explode(".", get_site_url() ) ;
    
    $new_url = "https://".$this_subdomain.".".$site_url_array[1].".".$site_url_array[2] ;
    
    $length_new_url = strlen( $new_url  ) ; 
    
    	foreach($items as $item){
    
    		$link = substr( $item->url, $length_new_url ) ;
    		$item->url = $new_url .   $link ;
    
    	}
    
      return $items;
    
    }
    
    add_filter('wp_nav_menu_objects', 'change_menu');
Viewing 1 replies (of 1 total)
  • The topic ‘Wildcard Subdomain – Override get_site_url()’ is closed to new replies.