Guest User

Untitled

a guest
Nov 18th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. define( 'FORCE_SSL_ADMIN', TRUE );
  2.  
  3. define( 'FORCE_SSL_LOGIN', TRUE );
  4.  
  5. <IfModule mod_rewrite.c>
  6. RewriteEngine On
  7. RewriteCond %{SERVER_PORT} !^443$
  8. RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
  9. </IfModule>
  10.  
  11. /**
  12. * Plugin Name: Force SSL for specific pages
  13. * Description:
  14. * Author: Frank Bültge
  15. * Author URI: http://bueltge.de/
  16. * Version: 1.0.0
  17. */
  18.  
  19. ! defined( 'ABSPATH' ) and exit;
  20.  
  21. if ( ! function_exists( 'fb_force_ssl' ) ) {
  22.  
  23. add_filter( 'force_ssl' , 'fb_force_ssl', 1, 3 );
  24. function fb_force_ssl( $force_ssl, $id = 0, $utrl = '' ) {
  25. // A list of posts/page that should be SSL
  26. $ssl_posts = array( 22, 312 );
  27.  
  28. if ( in_array( $id, $ssl_posts ) )
  29. $force_ssl = TRUE;
  30.  
  31. return $force_ssl;
  32. }
  33.  
  34. } // end if func exists
  35.  
  36. add_action( 'template_redirect', 'fb_ssl_template_redirect', 1 );
  37. function fb_ssl_template_redirect() {
  38.  
  39. if ( is_page( 123 ) && ! is_ssl() ) {
  40.  
  41. if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
  42. wp_redirect(preg_replace('|^http://|', 'https://', $_SERVER['REQUEST_URI']), 301 );
  43. exit();
  44. } else {
  45. wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301 );
  46. exit();
  47. }
  48. } else if ( !is_page( 123 ) && is_ssl() && !is_admin() ) {
  49.  
  50. if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
  51. wp_redirect(preg_replace('|^https://|', 'http://', $_SERVER['REQUEST_URI']), 301 );
  52. exit();
  53. } else {
  54. wp_redirect('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301 );
  55. exit();
  56. }
  57. }
  58. }
  59.  
  60. add_filter( 'pre_post_link', 'fb_set_ssl_url', 10, 3 );
  61. function fb_set_ssl_url( $permalink, $post, $leavename ) {
  62.  
  63. if ( 123 == $post->ID )
  64. return preg_replace( '|^http://|', 'https://', $permalink );
  65.  
  66. return $permalink;
  67. }
  68.  
  69. function isSecure() {
  70. return
  71. (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
  72. || $_SERVER['SERVER_PORT'] == 443;
  73. }
  74.  
  75. add_action( 'template_redirect', 'fb_ssl_template_redirect', 1 );
  76. function fb_ssl_template_redirect() {
  77.  
  78. global $post;
  79.  
  80. //login = 8886
  81. //Pages clients
  82. $array_posts_ssl = array(8886);
  83. $array_posts_ssl_parents = array(8886);
  84.  
  85. if ( in_array($post->ID,$array_posts_ssl) ) {
  86.  
  87. if ( !isSecure() ) {
  88. wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301 );
  89. exit();
  90. }
  91.  
  92. } else {
  93.  
  94. if ( isSecure() ){
  95. wp_redirect('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] , 301 );
  96. exit();
  97. }
  98. }
  99.  
  100. }
  101.  
  102. add_action('wp','_my_custom_ssl_redirect'); // the 'wp' hook is the first place the post id is set.
  103. function _my_custom_ssl_redirect(){
  104. global $post,$wp; // get some global values.
  105.  
  106. $page_ids = array(2,123,321,456); // array of page ids we want to force to ssl.
  107.  
  108. 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.
  109.  
  110. wp_safe_redirect( // make sure we only redirect to "internal" urls.
  111. add_query_arg( // add any url query arguments back to the url.
  112. $_SERVER['QUERY_STRING'], // The current query args.
  113. '',
  114. trailingslashit( // add a trailing slash to the home url as sometimes it is not added.
  115. home_url( $wp->request, "https" ), // get the home url HTTPS link.
  116. 301 // set the redirect to be 301 "permanent", you can use 302 "temporary" here instead.
  117. )
  118. )
  119. );
  120. exit; // exit ASAP, no point in loading anything more.
  121. }
  122. }
  123.  
  124. add_action('wp','_my_custom_ssl_redirect');
  125. function _my_custom_ssl_redirect(){
  126. global $post,$wp;
  127.  
  128. $page_ids = array(2,123,321,456); // array of page ids we want to force to ssl.
  129.  
  130. if( is_page() && isset($post->ID) && in_array($post->ID,$page_ids) ){
  131. wp_safe_redirect( add_query_arg( $_SERVER['QUERY_STRING'], '',trailingslashit(home_url( $wp->request, "https" ), 301 )) );
  132. exit;
  133. }
  134. }
Add Comment
Please, Sign In to add comment