Raju Vishwas
Raju Vishwas

Reputation: 2561

Wordpress get_template_directory_uri() returns http instead of https

We have recently applied SSL certificate on our website and we want all our url to have https:// protocol.

Once we moved our website to https://, our website broke down as there were few resource which were still pointing to http://. After researching for a while, I saw that get_template_directory_uri() is always returning http:// even through our wp_home, wp_site_url is set with https://

Is there any other place where we have to change the URL, as we are using child theme and this function get the parent theme directory.

Thanks, Raju Vishwas

Upvotes: 18

Views: 24278

Answers (8)

publikz.com
publikz.com

Reputation: 961

if( isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO']="https" ) $_SERVER['HTTPS']="on";

helps in my case in wp-config.php

My server not returns this server header in https:// urls!

Upvotes: 0

Zatakk
Zatakk

Reputation: 51

This worked for me like a charm, added to wp-config at the end:

define('FORCE_SSL_ADMIN', true);
define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST']);
define('WP_SITEURL', WP_HOME);

Upvotes: 5

hawbsl
hawbsl

Reputation: 16023

Check the list of plugins installed. Any plugin involving caching, CDNs etc may interfere with get_template_directory_uri. In our case the customer had installed a ton of plugins to supposedly speed up his site.

With no plugins enabled https loaded fine. So it was just a case of figuring out which one it was. And buried in the offending plugin's settings there was an http version of the site path.

Upvotes: 0

kakoma
kakoma

Reputation: 1203

Going by this comment the initiator added:

in my case we were using Load Balancer server and the SSL certificate was install on load balancer

The answer from @pbond scratches the surface to the root cause of the issue. The WordPress is_ssl() function checks the $_SERVER['HTTPS'] and $_SERVER['SERVER_PORT'] to check if the current page is being accessed via https but the load balancer is most likely requesting your content on the non-SSL port 80.

One good fix for this is to use the X-Forwareded-Proto HTTP header to figure out which protocol the client is actually using on the other side of the Load Balancer.

With Apache 2.2, you could add this to your configuration:

<IfModule mod_setenvif.c>
  SetEnvIf X-Forwarded-Proto "^https$" HTTPS
</IfModule>

Another possible fix (alluded to by @Roberto Poblete but not explained) is to add this to wp-config.php

if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
$_SERVER['HTTPS'] = 'on';

I have this to thank for sending me in the right direct

Upvotes: 7

pbond
pbond

Reputation: 1927

Check the $_SERVER['HTTPS'] value. This should be set to on or 1. If it has any other value while being set, this function will output http rather than https.

See: https://core.trac.wordpress.org/browser/tags/4.5.3/src/wp-includes/functions.php#L4025

Upvotes: 13

Roberto Poblete
Roberto Poblete

Reputation: 69

view notes of link https://codex.wordpress.org/Function_Reference/is_ssl

if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
$_SERVER['HTTPS'] = 'on';

Upvotes: 4

funkysoul
funkysoul

Reputation: 3241

Did you try to force content delivery through https with a modified .htaccess file?

below the snippet I'm using:

#Force HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Upvotes: 0

Atif Tariq
Atif Tariq

Reputation: 2772

Please use the following:

get_stylesheet_directory_uri();

it will be a web-address (starting with http:// or https:// for SSL). As such, it is most appropriately used for links, referencing additional stylesheets, or probably most commonly, images.

https://codex.wordpress.org/Function_Reference/get_stylesheet_directory_uri

Upvotes: -2

Related Questions