|
27 | 27 | return $overrides;
|
28 | 28 | }, 999 );
|
29 | 29 |
|
| 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 | + |
30 | 50 | // 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 | +} |
38 | 57 |
|
39 | 58 | // apply https redirect to all key areas
|
40 | 59 | foreach ( array( 'init', 'admin_init', 'login_init' ) as $hook ) {
|
|
0 commit comments