简体   繁体   中英

Wordpress get_template_directory_uri() and bloginfo('template_directory') return different results with HTTPS

I'm migrating a current WordPress 4.7.3 site to become full SSL and when I log into the site using https:// prefix the function calls to get_template_directory_uri() and bloginfo('template_directory') will skip the theme part of the path.

An example code line that I'm able to notice is this:

wp_enqueue_style('fancybox', get_template_directory_uri() . '/assets/css/fancybox/jquery.fancybox.css', false, null);

Another example code line would be this:

<img src="<?php bloginfo('template_directory'); ?>/assets/img/myimg.png" class="pull-left img-responsive"/>

This is the current working result sample excerpts in the order they appear on the result source code if I request the page with http://example.com

...
<link rel="stylesheet" href="http://example.com/wp-content/themes/mytheme/assets/css/fancybox/jquery.fancybox.css">
...
<img src="http://example.com/wp-content/themes/mytheme/assets/img/myimg.png" class="pull-left img-responsive"/>
...

This is then the same approach applied to the result calling with https://example.com

...
<link rel="stylesheet" href="https://example.com/assets/css/fancybox/jquery.fancybox.css">
...
<img src="https://example.com/assets/img/myimg.png" class="pull-left img-responsive"/>
...

I've recently migrated other sites with same WP version and legacy templates that use the same functions and they just work so that's why I'm intrigued if this is by any chance a specific wp_option or other setting that would be causing this behavior that I'm not being able to figure out.

For clarity, both siteurl and home are set to https://example.com and, of course, this is not the actual domain name. This is a clean WP install that has no changes on it's core, only a theme and couple plugins (askimet, S3 uploads) which shouldn't conflict.

Any clues on why the /wp-content/themes/mytheme is been skipped on the https:// call?

It ends up that this has been happening due to a couple of configurations on the theme:

add_theme_support('root-relative-urls');    // Enable relative URLs
add_theme_support('rewrites');              // Enable URL rewrites

After removing that I was able to get the expected result and the site now works with SSL using https:// .

In order to find that, I had to compare every file from other templates that work and by trial and errors got to this.

Hope it helps someone in the future not to get too much time wasted on a similar issue!

If you replace get_template_directory_uri() with bloginfo('template_directory') , it's normal that you're getting the path without the theme path because the function bloginfo doesn't return any value, it simply echo the value. Side node, if you view the page source, you should probably see somewhere in the DOM:

https://example.com/example.com/wp-content/themes/mytheme

Therefore, when you enqueue the style, it's only enqueuing '/assets/css/fancybox/jquery.fancybox.css' .

What you want is get_bloginfo instead which will return the value instead of echo:

wp_enqueue_style('fancybox', get_bloginfo('template_directory') . '/assets/css/fancybox/jquery.fancybox.css', false, null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM