Making SSL Work Properly on WordPress for One Page

I recently added a checkout page that required ssl to a website. I used Yoast’s WordPress SSL Setup blog post as a guide to get it going, and it works great, but there was a small problem. The permalinks for the page in question are still https. This caused some problems for my website. Any other page I would visit after the checkout page would be broken because of the SSL.

Because of this, I had to make a quick update to the code. Joost’s code is:

function yst_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_action( 'template_redirect', 'yst_ssl_template_redirect', 1 );

and

function yst_checkout_page_ssl( $permalink, $post, $leavename ) {
	if ( 123 == $post->ID ) {
		return preg_replace( '|^http://|', 'https://', $permalink );
	}
	return $permalink;
}
add_filter( 'pre_post_link', 'yst_checkout_page_ssl', 10, 3 );

the last bit of code changes permalinks for the permalink to https for the checkout page. However, it is missing the bit to change them back to http if it not the checkout page.

I changed the last part of my code to:

function yst_checkout_page_ssl( $permalink, $post, $leavename ) {
	if ( 123 == $post->ID ) {
		return preg_replace( '|^http://|', 'https://', $permalink );
	}
	return preg_replace( '|^https://|', 'http://', $permalink );
}
add_filter( 'pre_post_link', 'yst_checkout_page_ssl', 10, 3 );

This fixed my problem. Now, all the links on the checkout page point to the non secured version of the site.

I hope this helps people having the same problem as I did. Thanks to Joost for a great guide.

Leave a comment

Your email address will not be published. Required fields are marked *