Ask your WordPress questions! Pay money and get answers fast! (more info)

Function to register styles for SSL WordPress

  • SOLVED

Please can someone give me a way to register enqueue my styles but in the event that the page is SSL it loads another set of styles?

I currently use:



if ( !function_exists( 'my_main_styles' ) ) {
function my_main_styles() {

wp_register_style('simpleGrid', get_template_directory_uri() . '/simpleGrid.css');
wp_register_style("main-styles", get_stylesheet_directory_uri() . "/style.css");

wp_enqueue_style('simpleGrid');
wp_enqueue_style('main-styles');
}
}
add_action('wp_print_styles', 'my_main_styles');


so if it is SSL it should load 2 different style sheets called:

wp_register_style('simpleGrid', get_template_directory_uri() . '/simpleGrid-https.css');
wp_register_style("main-styles", get_stylesheet_directory_uri() . "/style-https.css");

and when these are printed in the header they should be forced to https so that they don't trigger warnings.

Answers (2)

2014-02-15

Arnav Joy answers:

try this

<?php


if ( !function_exists( 'my_main_styles' ) ) {

function my_main_styles() {

if(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443){

wp_register_style('simpleGrid', str_replace( 'http','https',get_template_directory_uri() ). '/simpleGrid-https.css');
wp_register_style("main-styles", str_replace( 'http','https',get_template_directory_uri() ). . "/style-https.css");

}
else{


wp_register_style('simpleGrid', get_template_directory_uri() . '/simpleGrid.css');

wp_register_style("main-styles", get_stylesheet_directory_uri() . "/style.css");

}




wp_enqueue_style('simpleGrid');

wp_enqueue_style('main-styles');

}

}

add_action('wp_print_styles', 'my_main_styles');


julesphoto comments:

That's seems to detect http or https but the url it generates is wrong:

<link rel='stylesheet' id='main-styles-css' href='https://www.domain.comhttpss://www.domain.com/wp-content/themes/example/style-https.css?ver=3.6.1' type='text/css' media='all' />


Arnav Joy comments:

try this

<?php


if ( !function_exists( 'my_main_styles' ) ) {

function my_main_styles() {

if(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443){

$new_url = str_replace( 'http','https',get_template_directory_uri() );

wp_register_style('simpleGrid', str_replace( 'http',$new_url. '/simpleGrid-https.css');
wp_register_style("main-styles", str_replace( 'http',$new_url . "/style-https.css");

}
else{


wp_register_style('simpleGrid', get_template_directory_uri() . '/simpleGrid.css');

wp_register_style("main-styles", get_stylesheet_directory_uri() . "/style.css");

}




wp_enqueue_style('simpleGrid');

wp_enqueue_style('main-styles');

}

}

add_action('wp_print_styles', 'my_main_styles');


julesphoto comments:

syntax error, unexpected ';' on this line:

wp_register_style('simpleGrid', str_replace( 'http', $new_url . '/simpleGrid-https.css');


Arnav Joy comments:

ohh sorry

try these two


wp_register_style('simpleGrid', $new_url. '/simpleGrid-https.css');
wp_register_style("main-styles", $new_url . "/style-https.css");


julesphoto comments:

That still has the same issue of inserting the link wrong:

<link rel='stylesheet' id='main-styles-css' href='https://www.domain.comhttpss://www.domain.com/wp-content/themes/example/style-https.css?ver=3.6.1' type='text/css' media='all' />


julesphoto comments:

$protocol = is_ssl() ? 'https' : 'http';


????


julesphoto comments:

or
wp_redirect(preg_replace('|^https://|', 'http://',


Arnav Joy comments:

in my system it is working fine .

this problem is only for "main-styles-css" or for both?

are you sure there is no other code or plugin is installed to modify url?


julesphoto comments:

The problem is for both links.

I have this at the top of my https header.

<?php if($_SERVER["HTTPS"] != "on") {

header("HTTP/1.1 301 Moved Permanently");

header("Location: https://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]);

exit();

}
?>


Arnav Joy comments:

try removing these lines once and check


julesphoto comments:

If I remove those lines it works normally as the page is no longer being forced to SSL. So it gives me the http versions of the stylesheets.


Arnav Joy comments:

try this , with your code enabled

$new_url = '/wp-content/themes/example';

wp_register_style('simpleGrid', $new_url. '/simpleGrid-https.css');

wp_register_style("main-styles", $new_url . "/style-https.css");


julesphoto comments:

Thanks I figured it out!!

2014-02-15

Hariprasad Vijayan answers:

Try this,


if ( !function_exists( 'my_main_styles' ) ) {
function my_main_styles() {
wp_register_style('simpleGrid', get_template_directory_uri() . '/simpleGrid.css');
wp_register_style("main-styles", get_stylesheet_directory_uri() . "/style.css");
wp_enqueue_style('simpleGrid');
wp_enqueue_style('main-styles');
}
add_action('wp_enqueue_scripts', 'my_main_styles',99);
}


julesphoto comments:

This doesn't switch style sheets depending on http or https.