Skip to content

Instantly share code, notes, and snippets.

@DevinWalker
Forked from dougedgington/functions.php
Last active May 13, 2017 16:11
Show Gist options
  • Save DevinWalker/7126128 to your computer and use it in GitHub Desktop.
Save DevinWalker/7126128 to your computer and use it in GitHub Desktop.
WooCommerce force SSL entire shop
<?php
/*
Author: Doug Edgington
Description: modified version of Woocomemrce SSL functionality, forces ssl on Woocommerce pages and two additional custom pages
*/
function dee_ssl_template_redirect() {
if ( ! is_ssl() ) {
if ( is_checkout() || is_account_page() || is_woocommerce() ) {
if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
wp_safe_redirect( preg_replace( '|^http://|', 'https://', $_SERVER['REQUEST_URI'] ) );
exit;
} else {
wp_safe_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
exit;
}
}
}
// Break out of SSL if we leave woocommerce pages or custom pages
elseif ( is_ssl() && $_SERVER['REQUEST_URI'] && ! is_checkout() && ! is_page( woocommerce_get_page_id('thanks') ) && ! is_ajax() && ! is_account_page()) {
if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
wp_safe_redirect( preg_replace( '|^https://|', 'http://', $_SERVER['REQUEST_URI'] ) );
exit;
} else {
wp_safe_redirect( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
exit;
}
}
} //end function
add_action( 'template_redirect', 'dee_ssl_template_redirect', 1 );
?>
@DevinWalker
Copy link
Author

If you want to force SSL on your entire WooCommerce shop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment