@@ -34,16 +34,21 @@ function force_https_filter_home( $value ) {
34
34
add_filter ( 'pre_option_home ' , 'force_https_filter_home ' );
35
35
add_filter ( 'pre_option_siteurl ' , 'force_https_filter_home ' );
36
36
37
- // force https redirect on frontend, admin, and login
37
+ // enforce https by redirecting non-ssl requests on frontend, admin, and login pages
38
38
function force_https_redirect () {
39
- if ( ! is_ssl () && ! defined ( 'WP_CLI ' ) && ! headers_sent () ) {
40
- wp_redirect ( set_url_scheme ( home_url ( $ _SERVER ['REQUEST_URI ' ] ), 'https ' ), 301 );
41
- exit ;
39
+
40
+ // exit if already using https, headers are sent, or running via cli
41
+ if ( is_ssl () || headers_sent () || defined ( 'WP_CLI ' ) ) {
42
+ return ;
42
43
}
44
+
45
+ // redirect to https version of the requested url with a permanent redirect
46
+ wp_redirect ( set_url_scheme ( home_url ( $ _SERVER ['REQUEST_URI ' ] ), 'https ' ), 301 );
47
+ exit ;
43
48
}
44
49
45
- // apply https redirect to all key areas
46
- foreach ( array ( 'init ' , 'admin_init ' , 'login_init ' ) as $ hook ) {
50
+ // apply https redirect during initialization, admin, and login
51
+ foreach ( [ 'init ' , 'admin_init ' , 'login_init ' ] as $ hook ) {
47
52
add_action ( $ hook , 'force_https_redirect ' , 10 );
48
53
}
49
54
0 commit comments