How to Increase HTTP Request Timeout in WordPress

If you need to test drive WordPress, Try it now!

Sharing is caring!

When developing plugins or themes that interact with third-party APIs—whether for payments, shipping, or data imports—WordPress developers often face timeout issues. These occur when a remote server takes too long to respond, and WordPress cancels the request before it completes.

The good news? WordPress gives you a few flexible ways to control request timeout values so you can avoid unnecessary failures due to slow—but still functional—external services.

Let’s explore how to increase the timeout using two different filters: http_request_timeout and http_request_args.


What Is a Timeout in WordPress HTTP Requests?

When WordPress sends a remote HTTP request (using functions like wp_remote_get() or wp_remote_post()), it waits a limited amount of time for the external server to respond. If that time limit is exceeded, the request is “timed out,” and WordPress stops waiting.

This is a useful safety mechanism—it prevents your site from hanging if a third-party service is down or too slow. But sometimes the default timeout is just too short, especially when the external API is under load or processing large data.

How WordPress Sends HTTP Requests

$response = wp_remote_get('https://example.com/data');

$response = wp_remote_post('https://example.com/submit', [
    'body' => [
        'name' => 'John',
        'email' => 'john@example.com',
    ]
]);

These functions rely on the underlying WordPress HTTP API, which supports filters for modifying request behavior—like timeouts.

Method 1: Conditionally Adjust Timeout Using http_request_timeout

If you want to only adjust the request timeout (not other settings like headers or the request body), this is the cleanest way.

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

/**
 * Set HTTP request timeout based on target URL
 */
function wpsandbox_tutorial_custom_timeout($timeout, $url) {
    if (strpos($url, 'example.com') !== false) {
        return max(60, $timeout); // Set to 60 seconds if less
    }

    return $timeout;
}

This filter:

  • Runs before the request is made
  • Checks if the URL matches a specific domain (example.com)
  • Increases the timeout if needed

Use this when you're only interested in the timeout value and want lightweight logic.

Method 2: Modify Timeout and Other Settings Using http_request_args

If you need more granular control, such as setting a connect_timeout or modifying other arguments, use this filter instead.

add_filter('http_request_args', 'wpsandbox_tutorial_change_timeout', 10, 2);

/**
 * Conditionally increase HTTP request timeout for example.com API
 */
function wpsandbox_tutorial_change_timeout($args, $url) {
    if (strpos($url, 'example.com') !== false) {
        $current_timeout = !empty($args['timeout']) ? (int) $args['timeout'] : 0;
        $args['timeout'] = max(60, $current_timeout);

        if (empty($args['connect_timeout'])) {
            $args['connect_timeout'] = 30;
        }
    }

    return $args;
}

This approach:

  • Lets you modify both the timeout and connection timeout
  • Can be extended to adjust headers, body, or method type if needed
  • Is useful when working with slow but reliable external APIs

Which One Should You Use?

ScenarioUse http_request_timeoutUse http_request_args
You only need to change the timeout✅ Yes❌ Not necessary
You want to target a specific domain✅ Yes✅ Yes
You need to set connect_timeout or other settings❌ No✅ Yes
You’re building complex API logic❌ Limited✅ Full control

Final Notes

  • Increasing timeout doesn’t fix slow APIs—it just prevents premature failure when the API is working, but slow.
  • Always test thoroughly, especially if your plugin depends on external services.
  • Don’t use long timeouts to mask actual problems like DNS failures or incorrect endpoints.

Sharing is caring!

If you need to test drive WordPress, Try it now!
This code shared in this post/site is provided "AS IS" without any warranties or guarantees of any kind. The authors and distributors of this code cannot be held responsible for any damage, data loss, or other issues that may arise from its use. It is strongly recommended that you test this code in a staging environment before deploying it to a live site. Always ensure you have proper backups and understand the implications of running this code on your WordPress installation. Use at your own risk.

Leave a Comment

Your email address will not be published. Required fields are marked *

The new new server is up and running. Enjoy!