Skip to content

Commit 73c3622

Browse files
authored
Update force-https.php
1 parent a5d13bc commit 73c3622

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

force-https.php

+26-7
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,33 @@
2727
return $overrides;
2828
}, 999 );
2929

30+
// ensure siteurl and home options are always https
31+
function force_https_fix_options() {
32+
// only update database if constants are not set
33+
if ( ! defined( 'WP_HOME' ) ) {
34+
update_option( 'home', set_url_scheme( get_option( 'home' ), 'https' ) );
35+
}
36+
37+
if ( ! defined( 'WP_SITEURL' ) ) {
38+
update_option( 'siteurl', set_url_scheme( get_option( 'siteurl' ), 'https' ) );
39+
}
40+
}
41+
add_action( 'init', 'force_https_fix_options', 1 );
42+
43+
// enforce https dynamically via filters (does not modify database)
44+
function force_https_filter_home( $value ) {
45+
return set_url_scheme( $value, 'https' );
46+
}
47+
add_filter( 'pre_option_home', 'force_https_filter_home' );
48+
add_filter( 'pre_option_siteurl', 'force_https_filter_home' );
49+
3050
// force https redirect on frontend, admin, and login
31-
function force_https_redirect() {
32-
// skip if already ssl, running wp-cli, or headers already sent
33-
if ( ! is_ssl() && ( ! defined( 'WP_CLI' ) || ! WP_CLI ) && ! headers_sent() ) {
34-
wp_safe_redirect( set_url_scheme( home_url( $_SERVER['REQUEST_URI'] ), 'https' ), 301 );
35-
exit;
36-
}
37-
}
51+
function force_https_redirect() {
52+
if ( ! is_ssl() && empty( $_SERVER['HTTPS'] ) && ! defined( 'WP_CLI' ) && ! headers_sent() ) {
53+
wp_safe_redirect( set_url_scheme( home_url( $_SERVER['REQUEST_URI'] ), 'https' ), 301 );
54+
exit;
55+
}
56+
}
3857

3958
// apply https redirect to all key areas
4059
foreach ( array( 'init', 'admin_init', 'login_init' ) as $hook ) {

0 commit comments

Comments
 (0)