Skip to content

Instantly share code, notes, and snippets.

@bearded-avenger
Created December 19, 2013 16:41
Show Gist options
  • Save bearded-avenger/8042261 to your computer and use it in GitHub Desktop.
Save bearded-avenger/8042261 to your computer and use it in GitHub Desktop.
Do SSL only on checkout page in Wordpress.
<?php
/**
* SSL Controls
*/
class flackerSSLControl {
function __construct(){
add_action( 'template_redirect', array($this,'url_change' ),1);
add_filter( 'pre_post_link', array($this,'checkout_ssl'),10, 3) ;
}
function url_change(){
$opts = get_option('flacker_options') ? get_option('flacker_options') : false;
$id = isset($opts['flacker_checkout_page']) ? $opts['flacker_checkout_page'] : false;
if ( is_page( $id ) && ! is_ssl() && $id ) {
if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
wp_redirect(preg_replace('|^http://|', 'https://', $_SERVER['REQUEST_URI']), 301 );
exit();
} else {
wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301 );
exit();
}
} else if ( !is_page( $id ) && is_ssl() && !is_admin() ) {
if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
wp_redirect(preg_replace('|^https://|', 'http://', $_SERVER['REQUEST_URI']), 301 );
exit();
} else {
wp_redirect('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301 );
exit();
}
}
}
function checkout_ssl( $permalink, $post, $leavename ) {
$opts = get_option('flacker_options') ? get_option('flacker_options') : false;
$id = isset($opts['flacker_checkout_page']) ? $opts['flacker_checkout_page'] : false;
if ( $id == $post->ID )
return preg_replace( '|^http://|', 'https://', $permalink );
return $permalink;
}
}
new flackerSSLControl;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment