Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get_template_directory_uri() returns http instead of https on SSL site

Tags:

ssl

wordpress

I know the topic has been already discussed, but it seems no solution was found. This is my case: I have a wordpress site which now has SSL. However, in the theme resources are added as follows:

wp_enqueue_script( 'spectrumwp-conditional', get_template_directory_uri() . '/js/vendor/conditional.js', array('jquery'), null, true);

but get_template_directory_uri() returns the url with http not https.

Can you help me to solve this?

Edit: I added this to my wp-config.php file

$_SERVER['HTTPS']='on';

Links to resources are shown with https:// but instead of, for example

https://www.exaple.com/wp-content/plugins/...

I have

https://www.example.com/plugins/LayerSlider/...

e.g. wp-content folder is missing

Final update:

uncommenting

 define('FORCE_SSL_ADMIN', true);

and adding

if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
 {  $_SERVER['HTTPS']='on'; }

just before the line

/* That's all, stop editing! Happy blogging. */

fixed the problem! Only, I recommend $_SERVER['HTTP_X_FORWARDED_PROTO'] is not used, as it may not be accurate enough.

Solved!

like image 880
Dimentica Avatar asked Jan 21 '17 10:01

Dimentica


1 Answers

You need to use is_ssl() to check if the site is running https:// or http://

Here is the hook to check and redirect :

function check_if_https() {
if ( !is_ssl() ) {
    wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301 );
    exit();
  }
}
add_action ( 'template_redirect', 'check_if_https', 1 );
like image 105
Mukesh Ram Avatar answered Nov 11 '22 04:11

Mukesh Ram