SSL Switcher

<?php
//Define to activate system
define('TER_SSL','https'); //'https' for a mostly secure site, 'http' for mostly unsecured

function ter_ssl_meta_box(){
global $post;
$disabled_post_types = array('attachment','revision','nav_menu_item','post');
if(in_array(get_post_type($post),$disabled_post_types )) return;
if(TER_SSL == 'https') $checkbox_text = 'Not Secured';
else $checkbox_text = 'Secured';
echo '<div class="misc-pub-section misc-pub-section-last">';
wp_nonce_field(plugin_basename(__FILE__),'ter_ssl_nonce');
$ter_ssl_meta = get_post_meta($post->ID,'_ter_ssl_meta',true);
if($ter_ssl_meta) $checked = ' checked="checked"';
echo '<input type="checkbox" name="ter_ssl_meta" id="ter-ssl-meta" value="checked"' . $checked . '> ' . $checkbox_text;
echo '</div>';
}
if(TER_SSL) add_action('post_submitbox_misc_actions','ter_ssl_meta_box');

function ter_ssl_save_meta_box($post_id){
if(!isset($_POST['post_type'])) return $post_id;
$disabled_post_types = array('attachment','revision','nav_menu_item');
if(in_array($_POST['post_type'],$disabled_post_types)) return $post_id;
if(!wp_verify_nonce($_POST['ter_ssl_nonce'],plugin_basename(__FILE__))) return $post_id;
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
if(!current_user_can('edit_post',$post_id)) return $post_id;
if($_POST['ter_ssl_meta'] == 'checked') update_post_meta( $post_id, '_ter_ssl_meta', $_POST['ter_ssl_meta'] );
else delete_post_meta( $post_id, '_ter_ssl_meta');
}
if(TER_SSL) add_action('save_post','ter_ssl_save_meta_box');

function ter_ssl_template_redirect(){
//Add funciton call to top of header.php, or use a wordpress action
if(!TER_SSL) return;
global $post;
$ter_ssl_meta = get_post_meta($post->ID,'_ter_ssl_meta',true);
if(!$ter_ssl_meta) return;
if(TER_SSL == 'https' && $_SERVER['HTTPS'] == 'on'){ wp_redirect('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'],301); exit; }
elseif(TER_SSL == 'http' && $_SERVER['HTTPS'] != 'on'){ wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'],301); exit; }
}

function ter_ssl_content_filter($content){
if(isset($_SERVER['HTTPS'])) $content = ereg_replace('http://' . $_SERVER['SERVER_NAME'],'https://' . $_SERVER['SERVER_NAME'],$content);
return $content;
}
if(TER_SSL) add_filter('the_content','ter_ssl_content_filter');

?>

0 Comments

You must be logged in to post a comment.