Skip to content

Instantly share code, notes, and snippets.

@dougedgington
Last active December 20, 2015 12:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dougedgington/6135573 to your computer and use it in GitHub Desktop.
Save dougedgington/6135573 to your computer and use it in GitHub Desktop.
Force SSL on Woocommerce pages and two additional custom pages
<?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_page(48) || is_page(64) ) {
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() && ! is_page(48) && ! is_page(64) ) {
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 );
?>
@Acorath
Copy link

Acorath commented Nov 19, 2014

The original functions this was built from have since been changed.

You can now accomplish this using the filters "woocommerce_force_ssl_checkout" and "woocommerce_unforce_ssl_checkout" within your function.php or plugin.

function my_force_ssl_pages( $force_https ) {
  if( ! $force_https && is_page( array( 48, 64 ) ) ) {
    $force_https = true;
  }

  return $force_https;
}
add_filter( 'woocommerce_force_ssl_checkout', 'my_force_ssl_pages', 10, 1);

function my_unforce_ssl_pages( $force_http ) {
  if( $force_http && is_page( array( 48, 64 ) ) ) {
    $force_http = false;
  }

  return $force_http;
}
add_filter( 'woocommerce_unforce_ssl_checkout', 'my_unforce_ssl_pages', 10, 1);

There's no need to check is_ssl() here since that's done before the filters are applied.

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