Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

| Views: 480
Now that my blog is finally moved to a new host and SSL is fully set up, here's a quick note on how to make FlatPress work over HTTPS. By default, even if you change the Blog URL in the Options screen, FlatPress will not use that in all cases, the changes need to be done in the actual code too.

There is quite a lot of use of the BLOG_BASEURL constant in the code, instead of the $fp_config['general]['www'] variable. This is what causes the problem. The constant is defined much earlier in the code than the configuration settings files is loaded. The constant is also set to the HTTP_HOST value using the HTTP protocol.

What I did was change the defaults.php file to use https instead of http on the line that defines BLOG_BASEURL. I still don't like how this is done, but to fix it properly and force the base URL to be the same as the configured Blog URL would take more effort. I also defined a new constant BLOG_HTTPS_START_DT which specifies when SSL was enabled on the blog, more on this further down.

Here's the code I have:
 defaults.php
...
# use https protocol instead of default http
define('BLOG_BASEURL', 'http://'.$_SERVER['HTTP_HOST']. BLOG_ROOT);
# date when https switch over happened, so that old permalinks are generated
# with http to avoid breaking social media likes
define('BLOG_HTTPS_START_DT', '28 March 2015');
...


Now to explain the new constant I've defined. This specifies when the SSL switch-over happened and is required so that any URLs that the Pretty URLs plugin generates use the correct protocol to avoid breaking the social media links. I've noticed that my older blog posts 'lost' their Facebook likes if I used the https URL in the data-ref attribute for the like button, so this is a workaround for that. It was a similar situation for Google Plus and Twitter buttons.

To get that to work, I've replaced the permalink() function in plugin.prettyurls.php with the following:
 plug.prettyurls.php
function permalink($str, $id) {
global $fpdb, $post;
if (PRETTYURLS_TITLES)
$title = sanitize_title($post['subject']);
else
$title = $id;
$date = date_from_id($id);
$prettyLink = "20{$date['y']}/{$date['m']}/{$date['d']}/$title/";
// posts before the HTTPS switch over date are forced to use http
if (strtotime(BLOG_HTTPS_START_DT) >= $date['time']) {
return str_replace('https:', 'http:', $this->baseurl) . $prettyLink;
}
else {
return $this->baseurl . $prettyLink;
}
}




In my new function, I check if the date of the blog post is before or on the SSL switch-over date and change the URL to http if it is. This makes all the social media URLs revert to the old pre-SSL links.

The same change should be done for static page links too, but since those do not use the date of the static page, there is nothing to compare against. I left these as is for now, but may return to fix them too.

The last bit that needs to happen is to change the Blog URL in the Admin Options screen since that is used in some places too.

-i

Have comments or feedback on what I wrote? Please share them below! Found this useful? Consider sending me a small tip.
comments powered by Disqus
Other posts you may like...