0

I use subdomains with the same template and I try to use filter template_directory to replace serverName by $_SERVER['HTTP_HOST'] but it doesn't work .

The default page is www.website.com and the user must choose one of three spaces "move.website.com", "visit.website.com", "live.website.com". For each three spaces I created a subdomain ! I want to use the same template for each subdomain.

For that I redirect each subdomain to the template. Using $_SERVER['HTTP_HOST'] with the if condition inside index.php I can choose which file or code to display in fonction of subdomain . But now I must modify all links (bloginfo(template_directory)) to add subdomain at the beginning to url.

The relative url used by get_template_directory is still the same as default using filter - the subdomain isn't added :

add_filter( 'template_directory', function( $template_dir ){
    $subdomain = 'https://' . $_SERVER['HTTP_HOST'];
    return str_replace( 'https://website.com', $subdomain , $template_dir_uri );
});

I should get https://subdomain.website.com

11
  • 1
    The example you linked is using template_directory_uri. Jul 28, 2022 at 14:27
  • Yes I know but I have tested both because template_directory_uri doesn't modify the url . add_filter( 'template_directory', function( $template_dir ){ $subdomain = 'https://' . $_SERVER['HTTP_HOST']; return str_replace( 'https://website.com', $subdomain , $template_dir );}); and add_filter( 'template_directory_uri', function( $template_dir_uri ){ $subdomain = 'https://' . $_SERVER['HTTP_HOST']; return str_replace( 'https://website.com', $subdomain , $template_dir_uri );}); doesn't do anything .
    – imagIne
    Jul 28, 2022 at 14:41
  • 2
    Are you trying to change where the template is loaded from, or are you trying to change URLs in the templates? Jul 28, 2022 at 14:53
  • 1
    Your question jumps directly into what you're doing without giving much context, explaining why, or what you're trying to achieve by doing this. Can you edit your question to explain what the problem you're trying to solve is? Why do you want to do this? Where is that code located? The filter you're using has nothing to do with URLs so I'm unsure what your thinking is or intentions, and get_template_directory doesn't return a URL
    – Tom J Nowell
    Jul 28, 2022 at 15:22
  • 1
    It's also unclear what you meant by "I use subdomains with the same template" do you actually mean theme? A template is a PHP file loaded by WordPress, it doesn't have a URL. I'm going to make a guess, are you trying to make all the assets in your theme load from the same subdomain?
    – Tom J Nowell
    Jul 28, 2022 at 17:30

0

Your Answer

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