Skip to content

Instantly share code, notes, and snippets.

@developdaly
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save developdaly/74f0af268c04c69a9e92 to your computer and use it in GitHub Desktop.
Save developdaly/74f0af268c04c69a9e92 to your computer and use it in GitHub Desktop.
This snippet will check the current URL's protocol and if it matches http:// then it will redirect to the same URL but replace the protocol with https://
<?php
add_action( 'after_setup_theme', 'my_plugin_force_ssl' );
/**
* Force unencrypted URLs to redirect to an SSL encrypted version.
*/
function my_plugin_force_ssl() {
$protocol = stripos( $_SERVER['SERVER_PROTOCOL'],'https' ) === true ? 'https://' : 'http://';
if( $protocol == 'http://' ) {
$redirect_to = 'https://'. $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
wp_redirect( esc_url( $redirect_to ), 301 );
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment