Some of the slowest WordPress pages I’ve profiled weren’t slow because of the database or heavy themes. They were slow because, somewhere during page generation, a plugin was waiting for another company’s server to respond. An Instagram feed, a currency rate lookup, a shipping estimate, a review widget, a licence check. When the external service took two seconds to reply, the page took two seconds longer. When the service was down, the page sometimes hung for thirty seconds before giving up. The WordPress HTTP API, used carefully, prevents most of that.
Integrations with external services are a normal part of modern websites. The problem is calling them at the wrong time, without limits and without caching. WordPress provides a solid HTTP API for making requests, and a few patterns make the difference between integrations that are invisible to visitors and ones that make your site fragile. This article covers the WordPress HTTP API, sensible timeouts, error handling, caching and when to move calls into the background.
The basics of the HTTP API
WordPress wraps outgoing HTTP requests in a set of functions that handle transport details, SSL, redirects and proxies consistently across hosting environments:
- wp_remote_get for GET requests
- wp_remote_post for POST requests
- wp_remote_request for other methods
- wp_remote_head for HEAD requests
And helpers to read the response:
- wp_remote_retrieve_response_code
- wp_remote_retrieve_body
- wp_remote_retrieve_header
A basic request looks like this:
Always prefer these functions over raw curl or file_get_contents. They respect WordPress’s proxy settings, can be filtered and mocked in tests, and show up in debugging tools like Query Monitor.
Rule 1: Always set a sensible timeout
The default timeout for wp_remote_get is five seconds. Some plugins raise it to 30 or more. On a page request, that means a slow or unreachable service can hold up the visitor for that long.
Choose timeouts based on context:
- During a page request a visitor is waiting for: as short as possible, often two to three seconds, combined with caching so the call rarely happens at all
- In the admin, for actions the user initiated: a little longer, perhaps five to ten seconds, with a clear message if it fails
- In background jobs: longer timeouts are acceptable, because nobody is waiting
A failed request after two seconds with a graceful fallback is far better than a successful request after twenty.
Rule 2: Handle every failure
External services fail in many ways: DNS problems, connection timeouts, SSL errors, rate limits, server errors, invalid responses and changed formats. Your code must handle all of them without breaking the page.
Check for:
- is_wp_error on the response, for transport-level failures
- The HTTP status code, for errors like 401, 404, 429 and 500
- Valid, expected data after decoding
When something fails, decide on a fallback: show cached data, hide the widget, display a friendly message or use a default value. Never let an external failure produce a PHP error or a blank page.
Rule 3: Cache responses
Most external data doesn’t need to be fetched on every request. Exchange rates change daily. Social feeds can be a few minutes old. Review counts can be an hour old. Cache the response and reuse it.
Transients are the simplest way:
| if ( is_wp_error( $response ) | 200 !== wp_remote_retrieve_response_code( $response ) ) { |
|---|
This example adds a useful pattern: keeping a “last known good” copy in a non-autoloaded option. If the API fails after the transient expires, the site uses the last successful data instead of showing nothing.
With a persistent object cache such as Redis, transients are stored in memory and are very fast. Without one, they live in the options table, which is fine for moderate use.
The stampede problem
When a popular cached value expires, many simultaneous visitors may all find the cache empty and all call the external API at once. On busy sites, that can hammer the external service, trigger rate limits and slow down many requests at the same moment.
Mitigations:
- Refresh the cache in the background before it expires, using a scheduled task, so visitors never trigger the fetch.
- Use a short lock, such as a transient set before fetching, so only one request fetches while others use the stale value.
- Serve stale data while revalidating, a pattern where expired data is still served briefly while a fresh copy is fetched.
Rule 4: Move slow calls out of the page request
The best place for most external calls is not during a visitor’s page load at all. Options include:
- Scheduled refreshes: a cron job or Action Scheduler task fetches data every hour and stores it. Pages only read the stored data.
- Background processing: when an event happens, such as an order being placed, queue a background job to notify the external system rather than calling it during checkout.
- Client-side loading: for non-essential widgets, load the data with JavaScript after the page renders, so the main content isn’t delayed.
Checkout is the most important place to apply this. Calling CRMs, marketing tools or analytics APIs during checkout delays order confirmation and risks failed orders when those services are slow. Queue those calls instead.
Security considerations
- Validate URLs if they come from user input or settings, to prevent your server being used to make requests to internal addresses, known as server-side request forgery. wp_safe_remote_get rejects local and private network addresses and is the right choice for user-supplied URLs.
- Keep SSL verification on. The sslverify option defaults to true. Turning it off to fix certificate errors makes requests vulnerable to interception.
- Protect API keys. Store them in wp-config.php constants or environment variables where possible, not in the database, and never expose them in front-end JavaScript.
- Sanitise responses before outputting them. External data is untrusted input.
Debugging HTTP requests
Query Monitor lists every HTTP API call made during a page load, with the URL, response code, time taken and the calling plugin. It’s the quickest way to find slow external calls.
For logging in production, the http_api_debug action fires after each request and can record failures to a log file. Some hosts also block outgoing requests to certain destinations, which shows up as connection errors. If requests fail only on one server, check its firewall and DNS.
A real example: the shipping estimate that slowed every product page
A client’s product pages took more than three seconds to generate, even though the theme was light and the database fast. Profiling showed the delay came from a delivery estimate widget that called a courier’s API on every product page view to calculate delivery dates for the visitor’s pin code, falling back to a default location when none was known.
Most visitors never entered a pin code, so the plugin was repeatedly asking the courier API the same question for the default location. We changed it to cache the default estimate for each product’s shipping class for an hour, refreshed in the background, and to call the API only when a visitor entered their own pin code, through an AJAX request after the page had loaded. Product pages dropped to well under a second, and the courier API traffic fell by more than ninety percent.
Respect the other side’s limits
External APIs have rate limits, usage quotas and terms of service. Caching and background processing aren’t just good for your performance; they keep you within those limits. Read the API’s documentation for limits, handle 429 Too Many Requests responses by backing off rather than retrying immediately, and identify your site with a sensible user agent where the API expects one. Integrations that behave politely are much less likely to be blocked when traffic grows.
Testing integrations
Write code so the external call can be replaced in tests. The pre_http_request filter lets you return a fake response without making a real request, which makes it easy to test success, failure and malformed data scenarios in PHPUnit. Testing the failure paths is especially important, because they’re the ones that rarely happen on a developer’s machine and frequently happen in production.
Log failures, not successes
Logging every successful request creates noise. Log failures with enough context, such as the endpoint, status code and a short message, to diagnose problems later without exposing API keys or personal data in log files.
Frequently asked questions
Should I use wp_remote_get or Guzzle?
For WordPress plugins, the HTTP API is the standard choice and works across hosting environments. Guzzle is fine in bundled applications, but adds a dependency and can conflict with other plugins using different versions.
Why do external requests work locally but fail on the server?
Common causes include outgoing firewall rules, DNS problems, outdated CA certificates and hosts blocking certain ports. Check the error message from is_wp_error.
How long should I cache API responses?
As long as the data can reasonably be stale. Minutes for fast-changing data, hours or a day for rates, statistics and feeds.
Can I block all external requests for testing?
Yes. Define WP_HTTP_BLOCK_EXTERNAL as true in wp-config.php, with WP_ACCESSIBLE_HOSTS listing any allowed hosts.




