HTTP and HTTPS in WordPress

posted in: Uncategorized | 0

In the themes header.php include the following in the top of the file:

function https( $buffer ) {
if( stripos($_SERVER[‘SERVER_PROTOCOL’],’https’) === true ) {
return str_replace(‘http ://’, ‘https://’,str_replace(‘http :\/\/’, ‘https:\/\/’, str_replace(‘http %3A%2F%2F’, ‘https%3A%2F%2F’, $buffer)));
} else {
return $buffer;
}
}

ob_start( “https” );

In the themes footer.php include the following in the bottom of the file:

ob_end_flush();

If the admin part of WordPress also shall handle https and http then in the themes admin-header.php include the following in the top of the file:

function ahttps( $buffer ) {
if( stripos($_SERVER[‘SERVER_PROTOCOL’],’https’) === true ) {
return str_replace(‘http ://’, ‘https://’,str_replace(‘http :\/\/’, ‘https:\/\/’, str_replace(‘http %3A%2F%2F’, ‘https%3A%2F%2F’, $buffer)));
} else {
return $buffer;
}
}

ob_start( “ahttps” );

UPDATE 1. December 2015
It is even simpler you only need to put it into the wp-load.php which is found in the root directory of the WordPress installation

function https( $buffer ) {
if( stripos($_SERVER[‘SERVER_PROTOCOL’],’https’) === true ) {
return str_replace(‘http ://’, ‘https://’,str_replace(‘http :\/\/’, ‘https:\/\/’, str_replace(‘http %3A%2F%2F’, ‘https%3A%2F%2F’, $buffer)));
} else {
return $buffer;
}
}

ob_start( “https” );

The drawback of this simple solution is that you need to modify the file every time you update WordPress.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.