Using WordPress ‘allowed_http_request_hosts()’ PHP function

The allowed_http_request_hosts() WordPress PHP function marks certain redirect hosts as safe for HTTP requests. This function is attached to the ‘http_request_host_is_external’ filter.

Usage

$allowed_hosts = allowed_http_request_hosts( $is_external, $host );

In this example, $is_external is a boolean indicating if the host is external, and $host is the string name of the host. The function will return a list of allowed hosts.

Parameters

  • $is_external (bool) – This is a required parameter, indicating whether the host is external.
  • $host (string) – This is also a required parameter, representing the host’s name.

More information

See WordPress Developer Resources: allowed_http_request_hosts()

Examples

Basic Usage

$is_external = true;
$host = 'example.com';
$allowed_hosts = allowed_http_request_hosts( $is_external, $host );

// Now $allowed_hosts will contain the list of allowed hosts.

In this example, we have an external host ‘example.com’, and we use the function to get the list of allowed hosts.

Internal Host

$is_external = false;
$host = 'localhost';
$allowed_hosts = allowed_http_request_hosts( $is_external, $host );

// $allowed_hosts will now contain the list of allowed hosts.

Here, we’re working with an internal host ‘localhost’. The function still returns the list of allowed hosts.

Non-existent Host

$is_external = true;
$host = 'nonexistent.com';
$allowed_hosts = allowed_http_request_hosts( $is_external, $host );

// Even though the host doesn't exist, the function still returns a list of allowed hosts.

This code shows that even if the specified host doesn’t exist, the function returns the allowed hosts.

Empty Host

$is_external = true;
$host = '';
$allowed_hosts = allowed_http_request_hosts( $is_external, $host );

// Despite having an empty string for the host, the function still returns the list of allowed hosts.

This demonstrates that even with an empty string as the host name, the function still operates as expected.

Changing External Status

$is_external = false;
$host = 'example.com';
$allowed_hosts = allowed_http_request_hosts( $is_external, $host );

// The list of allowed hosts will be returned even if we change the external status of a host.

This shows that even if the external status of a host changes, the function can still be used to return the list of allowed hosts.