Using WordPress ‘http_request_timeout’ PHP filter

The http_request_timeout WordPress PHP Filter allows you to modify the timeout value for an HTTP request.

Usage

add_filter('http_request_timeout', 'your_custom_function', 10, 2);

function your_custom_function($timeout_value, $url) {
    // Your custom code here
    return $timeout_value;
}

Parameters

  • $timeout_value (float): Time in seconds until a request times out. Default is 5.
  • $url (string): The request URL.

More information

See WordPress Developer Resources: http_request_timeout

Examples

Increase the timeout value for a specific URL

Increase the timeout value to 10 seconds for requests made to example.com.

add_filter('http_request_timeout', 'increase_timeout_for_example', 10, 2);

function increase_timeout_for_example($timeout_value, $url) {
    if (strpos($url, 'example.com') !== false) {
        $timeout_value = 10;
    }
    return $timeout_value;
}

Decrease the timeout value for all requests

Decrease the timeout value to 3 seconds for all HTTP requests.

add_filter('http_request_timeout', 'decrease_timeout_globally', 10, 2);

function decrease_timeout_globally($timeout_value, $url) {
    $timeout_value = 3;
    return $timeout_value;
}

Set a different timeout for multiple URLs

Set different timeout values for requests made to example.com and example.org.

add_filter('http_request_timeout', 'set_different_timeouts', 10, 2);

function set_different_timeouts($timeout_value, $url) {
    if (strpos($url, 'example.com') !== false) {
        $timeout_value = 10;
    } elseif (strpos($url, 'example.org') !== false) {
        $timeout_value = 7;
    }
    return $timeout_value;
}

Increase the timeout value based on the request method

Increase the timeout value to 10 seconds for POST requests.

add_filter('http_request_timeout', 'increase_timeout_for_post_requests', 10, 2);

function increase_timeout_for_post_requests($timeout_value, $url) {
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $timeout_value = 10;
    }
    return $timeout_value;
}

Remove the timeout for a specific URL

Disable the timeout for requests made to no-timeout.example.com.

add_filter('http_request_timeout', 'remove_timeout_for_no_timeout_example', 10, 2);

function remove_timeout_for_no_timeout_example($timeout_value, $url) {
    if (strpos($url, 'no-timeout.example.com') !== false) {
        $timeout_value = 0;
    }
    return $timeout_value;
}