Force SSL with WordPress
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?
![9 Mind-Blowing Canvas Demos]()
The <canvas> element has been a revelation for the visual experts among our ranks. Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead. Here are nine unbelievable canvas demos that...
![fetch API]()
One of the worst kept secrets about AJAX on the web is that the underlying API for it, XMLHttpRequest, wasn't really made for what we've been using it for. We've done well to create elegant APIs around XHR but we know we can do better. Our effort to...
![jQuery Link Nudging]()
A few weeks back I wrote an article about MooTools Link Nudging, which is essentially a classy, subtle link animation achieved by adding left padding on mouseover and removing it on mouseout. Here's how to do it using jQuery:
The jQuery JavaScript
It's important to keep...
![Fix Anchor URLs Using MooTools 1.2]()
The administrative control panel I build for my customers features FCKEditor, a powerful WYSIWYG editor that allows the customer to add links, bold text, create ordered lists, and so on. I provide training and documentation to the customers but many times they simply forget to...
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.
Is it feasible to turn off cookies for secured pages? If so, how?
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.
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.
Works like a charm!