Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- define( 'FORCE_SSL_ADMIN', TRUE );
- define( 'FORCE_SSL_LOGIN', TRUE );
- <IfModule mod_rewrite.c>
- RewriteEngine On
- RewriteCond %{SERVER_PORT} !^443$
- RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
- </IfModule>
- /**
- * Plugin Name: Force SSL for specific pages
- * Description:
- * Author: Frank Bültge
- * Author URI: http://bueltge.de/
- * Version: 1.0.0
- */
- ! defined( 'ABSPATH' ) and exit;
- if ( ! function_exists( 'fb_force_ssl' ) ) {
- add_filter( 'force_ssl' , 'fb_force_ssl', 1, 3 );
- function fb_force_ssl( $force_ssl, $id = 0, $utrl = '' ) {
- // A list of posts/page that should be SSL
- $ssl_posts = array( 22, 312 );
- if ( in_array( $id, $ssl_posts ) )
- $force_ssl = TRUE;
- return $force_ssl;
- }
- } // end if func exists
- add_action( 'template_redirect', 'fb_ssl_template_redirect', 1 );
- function fb_ssl_template_redirect() {
- if ( is_page( 123 ) && ! is_ssl() ) {
- 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( 123 ) && 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();
- }
- }
- }
- add_filter( 'pre_post_link', 'fb_set_ssl_url', 10, 3 );
- function fb_set_ssl_url( $permalink, $post, $leavename ) {
- if ( 123 == $post->ID )
- return preg_replace( '|^http://|', 'https://', $permalink );
- return $permalink;
- }
- function isSecure() {
- return
- (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
- || $_SERVER['SERVER_PORT'] == 443;
- }
- add_action( 'template_redirect', 'fb_ssl_template_redirect', 1 );
- function fb_ssl_template_redirect() {
- global $post;
- //login = 8886
- //Pages clients
- $array_posts_ssl = array(8886);
- $array_posts_ssl_parents = array(8886);
- if ( in_array($post->ID,$array_posts_ssl) ) {
- if ( !isSecure() ) {
- wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301 );
- exit();
- }
- } else {
- if ( isSecure() ){
- wp_redirect('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] , 301 );
- exit();
- }
- }
- }
- add_action('wp','_my_custom_ssl_redirect'); // the 'wp' hook is the first place the post id is set.
- function _my_custom_ssl_redirect(){
- global $post,$wp; // get some global values.
- $page_ids = array(2,123,321,456); // array of page ids we want to force to ssl.
- if( is_page() && isset($post->ID) && in_array($post->ID,$page_ids) ){ // check we are on a page and its a page we want to redirect.
- wp_safe_redirect( // make sure we only redirect to "internal" urls.
- add_query_arg( // add any url query arguments back to the url.
- $_SERVER['QUERY_STRING'], // The current query args.
- '',
- trailingslashit( // add a trailing slash to the home url as sometimes it is not added.
- home_url( $wp->request, "https" ), // get the home url HTTPS link.
- 301 // set the redirect to be 301 "permanent", you can use 302 "temporary" here instead.
- )
- )
- );
- exit; // exit ASAP, no point in loading anything more.
- }
- }
- add_action('wp','_my_custom_ssl_redirect');
- function _my_custom_ssl_redirect(){
- global $post,$wp;
- $page_ids = array(2,123,321,456); // array of page ids we want to force to ssl.
- if( is_page() && isset($post->ID) && in_array($post->ID,$page_ids) ){
- wp_safe_redirect( add_query_arg( $_SERVER['QUERY_STRING'], '',trailingslashit(home_url( $wp->request, "https" ), 301 )) );
- exit;
- }
- }
Add Comment
Please, Sign In to add comment