-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy-aware-https-fix.php
More file actions
56 lines (50 loc) · 1.13 KB
/
Copy pathproxy-aware-https-fix.php
File metadata and controls
56 lines (50 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
* Plugin Name: Proxy-Aware HTTPS Fix
* Description: Enforces HTTPS detection and correct REST API resolution behind reverse proxies.
* Version: 1.4.1
* Author: BitCryptic
*
* @package Proxy_Aware_Https_Fix
*/
if ( defined( 'WP_CLI' ) && WP_CLI ) {
return;
}
add_action(
'plugins_loaded',
function () {
$proto = isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] )
? sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) )
: '';
if (
( ! isset( $_SERVER['HTTPS'] ) || 'on' !== $_SERVER['HTTPS'] ) &&
'https' === $proto
) {
$_SERVER['HTTPS'] = 'on';
}
}
);
add_filter(
'home_url',
function ( $url ) {
if ( ! empty( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ) {
return str_starts_with( $url, 'http:' ) ? 'https:' . substr( $url, 5 ) : $url;
}
return $url;
}
);
add_filter(
'site_url',
function ( $url ) {
if ( ! empty( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ) {
return str_starts_with( $url, 'http:' ) ? 'https:' . substr( $url, 5 ) : $url;
}
return $url;
}
);
add_filter(
'rank_math/rest_api_url',
function () {
return get_site_url( null, '/wp-json/' );
}
);