How to force use https insted of http for all javascript js file or stylesheet css files in wordpress

Just copy and paste below code into your theme's functions.php file

add_action('wp_print_scripts', 'rj_force_ssl_scripts', 100);
add_action('wp_print_styles', 'rj_force_ssl_styles', 100);


function rj_force_ssl_scripts() {
    if (!is_admin()) {
        if (!empty($_SERVER['HTTPS'])) // or you can also use is_ssl() function for check ssl
        {
            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 rj_force_ssl_styles() {
    if (!is_admin()) {
        if (!empty($_SERVER['HTTPS'])) // or you can also use is_ssl() function for check ssl
        {
            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);
            }
        }
    }
}

Comments

Popular posts from this blog

How to add a custom sorting or order by option on category archive or product archive page in woocommerce wordpress

How to create a custom wp_list_table and bulk action in wordpress

How to add image option in nav menu in wordpress