Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhwebworks/3cf728c5d5e0c021105f to your computer and use it in GitHub Desktop.
Save bhwebworks/3cf728c5d5e0c021105f to your computer and use it in GitHub Desktop.
Redirect specific WordPress pages or posts to https
/**
* 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;
}
@bhwebworks
Copy link
Author

@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 ) )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment