Last active
December 12, 2017 00:14
-
-
Save bhwebworks/3cf728c5d5e0c021105f to your computer and use it in GitHub Desktop.
Redirect specific WordPress pages or posts to https
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Redirect specific pages or posts to https | |
* | |
* This code assumes the page/post ID to make https is 7000. | |
* You will need to change that ID to match your site. | |
* | |
* @link http://blackhillswebworks.com/?p=5088 | |
*/ | |
add_action( 'template_redirect', 'bhww_front_end_ssl_template_redirect', 2 ); | |
function bhww_front_end_ssl_template_redirect() { | |
if ( is_single( 7000 ) && ! 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_single( 7000 ) && 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(); | |
} | |
} | |
} | |
// OPTIONAL - Make sure SSL page permalinks are also https - only applies to Posts, not Pages | |
add_filter( 'pre_post_link', 'bhww_make_page_permalinks_ssl', 10, 3 ); | |
function bhww_make_page_permalinks_ssl( $permalink, $post, $leavename ) { | |
if ( 7000 == $post->ID ) | |
return preg_replace( '|^http://|', 'https://', $permalink ); | |
return $permalink; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@abacoin,
Sorry I'm late - I just now saw your question.
You can use is_single() with an array, so it would look like this for the page IDs you mentioned:
is_single( array( 7,8,9 ) )