• I have some svg images, and some javascript code on a page that uses the get_template_directory_uri() function to generate a url.

    Site is setup with with settings for WordPress Address and Site Address using ‘www’ in the url. Browser address bar always displays ‘www’.

    Oddly, get_template_directory_uri() returns the url without ‘www’ at times. When this happens, the only thing that seems to solve the issue is to login to wp-admin. Once I log in, get_template_directory_uri() returns the correct url including the ‘www’. If I then log out, it will continue to return the correct url for a day or so. If I visit the site from a browser that has never logged in after logging in on another browser, I will also get the correct address from get_template_directory_uri(). The effect only seems to last a little while though. Within a day or so get_template_directory_uri() will start returning the url without ‘www’ again.

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    That is odd. However, the function tries to determine the correct URL through a number of processes depending upon the situation, so it’s not all that surprising. The thought of tracing through all the various situations to determine the root cause gives me a headache. I suggest instead using the “template_directory_uri” filter to force the subdomain to always be what you want regardless of what WP thinks it should be. This involves a little custom coding.

    Thread Starter shawn caza

    (@shawncaza)

    Thanks for the tip.

    I was able to solve the issue with this function:

    add_filter( 'template_directory_uri', 'filter_function_name_5627', 10, 3 );
    function filter_function_name_5627( $template_dir_uri, $template, $theme_root_uri ){
    	// filter...
        
        if(!strpos($template_dir_uri, 'localhost')){
            $bits = parse_url($template_dir_uri);
       
        if(!strpos($bits["host"], 'www')){
            
            $bits["host"] = 'www.'. $bits["host"];
        }
    
            $template_dir_uri = $bits["scheme"] . "://" . $bits["host"]. $bits["path"];
       }
        
    	return $template_dir_uri;
    }

    One other odd thing I noticed while implementing this function is that it also impacted how wordpress embeds the theme style sheet. Odd to me because my original problem wasn’t impacting the stylesheet for some reason. Just my use of get_template_directory_uri() within my theme.

    Thread Starter shawn caza

    (@shawncaza)

    Correction for the above.

    if(strpos($bits["host"], 'www')!=FALSE){

    Thread Starter shawn caza

    (@shawncaza)

    That should be ‘==’.

    • This reply was modified 4 years, 6 months ago by shawn caza.
    Thread Starter shawn caza

    (@shawncaza)

    if(strpos($bits[“host”], ‘www’)===FALSE){

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘get_template_directory_uri() sometimes returns url without ‘www’’ is closed to new replies.