How to Force Specific Pages to be Secure SSL HTTPS in WordPress

Home » Snippets » How to Force Specific Pages to be Secure SSL HTTPS in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

In today’s digital landscape, security is a top priority for website owners. One way to ensure the protection of sensitive information is by using SSL (Secure Sockets Layer) certificates, which establish an encrypted connection between the server and the user’s browser. While you may have already implemented SSL on your WordPress site, you might want to go one step further and force specific pages to be secure with HTTPS. By doing so, you can provide an extra layer of security for pages that handle sensitive data, such as login or payment pages. In this article, we will guide you through the process of forcing specific pages to be secure SSL HTTPS on your WordPress site, helping you create a safer browsing experience for your users.

					function wpturbo_force_ssl_for_specific_pages( $url ) {
    if ( is_page( array( 'page1', 'page2', 'page3' ) ) ) {
        if ( strpos( $url, 'http://' ) !== false ) {
            $url = str_replace( 'http://', 'https://', $url );
        }
    }
    return $url;
}
add_filter( 'wp_redirect', 'wpturbo_force_ssl_for_specific_pages' );
				

The code snippet provided demonstrates how to force specific pages to be accessed using secure SSL HTTPS. By using this code, you can ensure that certain pages on your WordPress website are always accessed securely.

First, we define a function called wpturbo_force_ssl_for_specific_pages() that takes in a URL parameter. This function is responsible for checking if the current page being accessed is on the list of pages we want to force to use HTTPS.

Inside the function, we use the is_page() function, which checks if the current page being accessed is one of the specified pages. In this example, the pages we want to force to use HTTPS are 'page1', 'page2', and 'page3'. You can modify this array to include the specific pages you want to force to use HTTPS.

If the current page is one of the specified pages and the URL contains http://, we use str_replace() to replace http:// with https://. This effectively changes the URL to use the secure HTTPS protocol.

Finally, the modified URL is returned by the function.

To ensure that the redirection to HTTPS takes place, we hook the wpturbo_force_ssl_for_specific_pages() function into the wp_redirect filter using the add_filter() function. This filter allows us to modify the URL before any redirection occurs, ensuring that the specified pages are forced to use HTTPS.

With this code snippet in place, any time a visitor tries to access one of the specified pages using http://, the URL will automatically be redirected to use https:// instead, providing a secure browsing experience for users.

Register an account to save your snippets or go Pro to get more features.