Solved:
Add these code to the functions.php file of your subsite's theme, immediately after the initial "
AND READ THIS, BEFORE USING:
https://codex.wordpress.org/Changing_The_Site_URL#Edit_functions.php
if ( ! is_admin() ) {
$xsiteurl = get_option( 'home' );
$find = array( 'http://', 'https://' );
if ( $find != 'http://' ) {
$replace = '';
$pureaddress = str_replace( $find, $replace, $xsiteurl );
$ssladdress = 'https://' . $pureaddress . '';
if ( $xsiteurl != $ssladdress ) {
update_option( 'siteurl', '' . $ssladdress . '' );
update_option( 'home', '' . $ssladdress . '' );
}
}
}
Notes:
Originally the best it would have been the get_option( 'siteurl' )
, that determine the "$xsiteurl", but this code deprecated, and the wordpress recommend that use it instead the site_url()
, because "equivalent functionality", BUT THIS FALSE:
If you use the get_site_url() on a multisite, what using https on .htaccess, wp-config.php and on the network home, such as WP_SITEURL and WP_HOME, the output of get_site_url()
will be https://subdomain.domain.com even if this subsite real (so/and default) site url and home options are: http://subdomain.domain.com. ERGO: get_site_url()
is not nearly equal with get_option( 'siteurl' )
...
Although your .htaccess redirect your subsite (what have http on own siteurl and home options) to https, and this will work fine, but will cause mixed content issue on your site, so than your network home have a link what shows to your subsite, your SSL will not work properly and to you need to correct this bad links in your database with example Better Search Replace plugin...
When I used get_option( 'home' );
this gives the REAL value of the home url option unlikely get_site_url()
.
Finally the if ( $xsiteurl != $ssladdress )
takes care of it that code dont running repeatedly (see in the wordpress codex link), so this safe...
The last "fly in the ointment", this code it only runs out on a site, if someone open the site (even wp admin, even the front-end)... so the new subsites default siteurl and home options they will remain http, and this code only rewrite this defaults after the code run out... (This maybe can cause mixed content problems if you creating a new subsite, and without that first open it this site's back or front-end, broadcasting posts on this from other subsites... )
postscript: I am only web designer and just now learning php, If you know something bad with this code, please write it down.
on
value to the$_SERVER
array element.$_SERVER
is dedicated to provide the information, not to set up the server environment. Documentation.