Using WordPress ‘pre_http_send_through_proxy’ PHP filter

The ‘pre_http_send_through_proxy’  WordPress filter lets you control whether an HTTP request should be sent through a proxy or not. By returning false, you can bypass the proxy, while returning true will send the request through the proxy. If you return null, the filter will be bypassed.

Usage

add_filter( 'pre_http_send_through_proxy', 'your_function_name', 10, 4 );

Parameters

  • $override (bool|null): Whether to send the request through the proxy. Default is null.
  • $uri (string): The URL of the request.
  • $check (array): An associative array containing the result of parsing the request URL with parse_url().
  • $home (array): An associative array containing the result of parsing the site URL with parse_url().

Examples

Bypass proxy for specific domain

function bypass_proxy_for_domain( $override, $uri, $check, $home ) {
    if ( 'example.com' === $check['host'] ) {
        return false;
    }
    return $override;
}
add_filter( 'pre_http_send_through_proxy', 'bypass_proxy_for_domain', 10, 4 );

In this example, the code checks if the request is for the domain “example.com”. If so, it bypasses the proxy by returning false. Otherwise, it returns the default $override value.

Always use proxy

function always_use_proxy( $override, $uri, $check, $home ) {
    return true;
}
add_filter( 'pre_http_send_through_proxy', 'always_use_proxy', 10, 4 );

This example ensures that all requests are sent through the proxy by always returning true.

Use proxy for external requests

function proxy_for_external_requests( $override, $uri, $check, $home ) {
    if ( $check['host'] !== $home['host'] ) {
        return true;
    }
    return $override;
}
add_filter( 'pre_http_send_through_proxy', 'proxy_for_external_requests', 10, 4 );

This code checks if the request is for an external domain by comparing the request’s host with the site’s host. If the hosts are different, it sends the request through the proxy.

Bypass proxy for specific URL path

function bypass_proxy_for_path( $override, $uri, $check, $home ) {
    if ( '/example-path/' === $check['path'] ) {
        return false;
    }
    return $override;
}
add_filter( 'pre_http_send_through_proxy', 'bypass_proxy_for_path', 10, 4 );

In this example, the code checks if the request URL path is “/example-path/”. If it is, the proxy is bypassed by returning false. Otherwise, the default $override value is returned.

Use proxy only for HTTPS requests

function proxy_for_https_requests( $override, $uri, $check, $home ) {
    if ( 'https' === $check['scheme'] ) {
        return true;
    }
    return $override;
}
add_filter( 'pre_http_send_through_proxy', 'proxy_for_https_requests', 10, 4 );

This code checks if the request is using the HTTPS scheme. If it is, the request is sent through the proxy by returning true. Otherwise, the default $override value is returned, allowing the request to be processed as usual. This example ensures that only HTTPS requests are sent through the proxy, while HTTP requests are not affected.