How To Force Use HTTPs instead Of HTTP For All Javascript Js File Or Stylesheet CSS Files In WordPress

Here we learn about how to force use HTTPs instead of HTTP for all Javascript js file or stylesheet CSS files in WordPress.

Custom code copied and paste into your child theme’s functions.php file.

<?php
add_action( 'wp_print_scripts', 'myblog_force_ssl_scripts', 100 );
add_action( 'wp_print_styles', 'myblog_force_ssl_styles', 100 );

function myblog_force_ssl_scripts(){
  if( !is_admin() ) {
    if( !empty( $_SERVER['HTTPS'] ) ){
      global $wp_scripts;
      foreach ( ( array ) $wp_scripts->registered as $script ){
        if ( stripos( $script->src, 'http://', 0 ) !== FALSE )
          $script->src = str_replace( 'http://', 'https://', $script->src );
      }
    }
  }
}

function myblog_force_ssl_styles() {
  if( !is_admin() ){
    if ( !empty( $_SERVER['HTTPS'] ) ){
      global $wp_styles;
      foreach( ( array ) $wp_styles->registered as $script ) {
        if( stripos($script->src, 'http://', 0) !== FALSE )
          $script->src = str_replace( 'http://', 'https://', $script->src );
      }
    }
  }
}
?>

 

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories