Force SSL with WordPress

By  on  

WordPress, the popular blogging CMS platform, is used as an all-purpose site software these days.  The difficulty in using all-purposes solutions is that they are often difficult to customize when edge cases pop up;  one of those edge cases can be forcing SSL.  Many form pages, for example, will be secured to gain user trust before filling them out.  WordPress provides an excellent method to secure individual pages!  Here's how you can force SSL within specific WordPress pages!

The PHP

To secure a specific WordPress post or page, you'll need to know its ID.  When you know its ID, it's securing the page is easy:

function force_ssl($force_ssl, $id = 0) {
	// A list of posts that should be SSL
	$ssl_posts = array(1, 12, 19);

	if(in_array($id, $ssl_posts)) {
		$force_ssl = true;
	}
    return $force_ssl;
}
add_filter('force_ssl' , 'force_ssl', 1, 3);

The force_ssl hook allows for us to check the post ID and force SSL if the post ID is in array of posts that should be secured!  Aren't WordPress hooks great to work with?

Recent Features

  • By
    6 Things You Didn’t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving...

  • By
    Create a CSS Flipping Animation

    CSS animations are a lot of fun; the beauty of them is that through many simple properties, you can create anything from an elegant fade in to a WTF-Pixar-would-be-proud effect. One CSS effect somewhere in between is the CSS flip effect, whereby there's...

Incredible Demos

  • By
    MooTools Wall Plugin

    One of the more impressive MooTools plugins to hit the Forge recently was The Wall by Marco Dell'Anna.  The Wall creates an endless grid of elements which can be grabbed and dragged, fading in elements as they are encountered.  Let me show...

  • By
    CSS content and attr

    CSS is becoming more and more powerful but in the sense that it allows us to do the little things easily.  There have been larger features added like transitions, animations, and transforms, but one feature that goes under the radar is generated content.  You saw a...

Discussion

  1. Keith Henry

    Be very careful securing some pages and not others on the same site – any cookies created in the secure area (for instance for login or user details) will be sent unencrypted to non-SSL pages, making interception attacks easy.

  2. Sunny

    Is it feasible to turn off cookies for secured pages? If so, how?

  3. Lynne

    Wondering what file you paste in the code above to secure a page/s with SSL.
    Also, are you saying that this piece of code:
    $ssl_posts = array(1, 12, 19); you simply replace those numbers with your page id?
    And with this code:
    add_filter(‘force_ssl’ , ‘force_ssl’, 1, 3); what is it’s purpose and do you also replace these numbers?
    I’d also be interested to know more about what Keith Henry has said about cookies potentially being sent unencrypted to non-SSL pages.

  4. Chris Waters

    Does this filter still exist? I can’t find any reference to it in the WordPress docs or code. As a result, I can’t seem to get this snippet working.

  5. Works like a charm!

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!