4

I'm using a third party JS file. And they have both http and https version. My site have some pages loading in https while most of pages are in http.

Now my question is could I do something in header.php so if my page is in https it will load the https version JS, otherwise load the http version?

Thank you!

11

If you link to the JS file just using something like //examplesite.com/js/jsfile.js without the http:// or https:// the browser should automatically get the correct http or https version

|improve this answer|||||
  • 4
    This is the correct way to do it, in all web development, not just WordPress. – Rylee Fowler Jan 17 '15 at 18:57
4

You can check if the site is SSL when enqueueing your JS files.

function theme_name_scripts() {
    if( is_ssl() ){
      # Running HTTPS
      wp_enqueue_script( 'https-js', get_template_directory_uri() . '/js/https.js );
    }
    else{
      # Not HTTPS
      wp_enqueue_script( 'http-js', get_template_directory_uri() . '/js/http.js );
    }
}

add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.