
How to Force SSL on Specific Pages of WordPress
- December 14, 2015
- Leave a comment
SSL stands for “Secure Socket Layer” and is used to secure your web pages. If its status is active in your web application then your browser will redirect your website to secure the connection for security purpose. SSL activated applications contain data in encrypted form.
This SSL may be integrated into your payment modules, for login purposes or whatever other reason contingent upon your application. It can be activated by writing a script in your web application. In WordPress, it can be activated through a filter named as “force_ssl”. If you want to allow SSL then this filter will automatically enforce SSL on your complete WordPress website. Your website will then redirect you to HTTPS connection.
If you want to allow SSL on specific WordPress pages then use the following code in your Child Theme or in functions.php file of your Theme:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
add_action( 'template_redirect', 'force_ssl_on_wp_pages', 1 ); function force_ssl_on_wp_pages() { $specific_page_id = array(819, 1142); // The are the id’s of pages $url=$_SERVER['REQUEST_URI']; $host=$_SERVER['HTTP_HOST']; if ( is_page( $specific_page_id ) && ! is_ssl() ) { if ( 0 === strpos($url, 'http') ) { wp_redirect(preg_replace('|^http://|', 'https://', $url), 301 ); exit(); } else { wp_redirect('https://' . $host . $url, 301 ); exit(); } } else if ( !is_page( 123 ) && is_ssl() && !is_admin() ) { if ( 0 === strpos($url, 'http') ) { wp_redirect(preg_replace('|^https://|', 'http://', $url), 301 ); exit(); } else { wp_redirect('http://' . $host . $url, 301 ); exit(); } } } |
This bit of code does NOT work – it actually locked me out of my admin completely. Thankfully I had a backup of my functions file to upload and overwrite. Don’t use!
Hi Margie,
We have rechecked that code at our side and its working fine. There might be some issue while placing that code or the conflict of the hook at your end. Or maybe you have not changed the page id’s at your end(mentioned in the code).
Regards,
—
Abdullah | WP Plugin Developer
Email: support@presstigers.com
Sorry but what is reason for certain page only? because SSL important for SEO now guys, I’m using the plugin too it good for redirects….. https://wordpress.org/plugins/force-https-littlebizzy/
I have a problem also with the code (did put in the correct page ID, and changed “&” to “&”). However, the browser says that it is going into an infinite re-direct loop, and stops with an error. Will try to debug this.