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?
![CSS Animations Between Media Queries]()
CSS animations are right up there with sliced bread. CSS animations are efficient because they can be hardware accelerated, they require no JavaScript overhead, and they are composed of very little CSS code. Quite often we add CSS transforms to elements via CSS during...
![7 Essential JavaScript Functions]()
I remember the early days of JavaScript where you needed a simple function for just about everything because the browser vendors implemented features differently, and not just edge features, basic features, like addEventListener and attachEvent. Times have changed but there are still a few functions each developer should...
![Introducing MooTools Dotter]()
It's best practice to provide an indicator of some sort when performing an AJAX request or processing that takes place in the background. Since the dawn of AJAX, we've been using colorful spinners and imagery as indicators. While I enjoy those images, I am...
![Control Element Outline Position with outline-offset]()
I was recently working on a project which featured tables that were keyboard navigable so obviously using cell outlining via traditional tabIndex=0 and element outlines was a big part of allowing the user navigate quickly and intelligently. Unfortunately I ran into a Firefox 3.6 bug...
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!