Cache External API Data in WordPress with Transients
Use the WordPress Transients API to cache expensive remote requests, with code examples and caveats about expiration and cache backends.
22 Aug 2025, 10:32 UTC

Your page is waiting on an external API
A common performance problem in WordPress is calling a remote API on every page load. Your theme or plugin might fetch weather data, exchange rates, or a third-party feed with wp_remote_get(). If that service takes 800 milliseconds to respond, every visitor waits. If the API slows down or rate-limits your server, your site degrades with it.
The useful takeaway: cache the remote response with the WordPress Transients API so the next visitor gets the stored result instead of a new network request.
What a transient actually does
The Transients API stores a value with an optional lifetime in seconds. set_transient( $key, $value, $expiration ) writes it, and get_transient( $key ) reads it back. When nothing is stored or the time has passed, get_transient() returns false.
By default WordPress keeps transients in the wp_options database table. If you run a persistent object cache plugin such as Redis or Memcached, transients live in that cache instead.
Expiration is a best-effort hint, not a guarantee. WordPress may delete expired transients during garbage collection or a cache flush, but an unused transient can outlive its timer. Do not rely on transients for security, billing, or critical state.
Worked example: cache a remote JSON response
This example fetches a public endpoint once per hour instead of on every page load. Add the code to a custom plugin or your theme's functions.php file. You need file-system access to edit WordPress code, and your hosting must allow outbound HTTP requests. Test on a local or staging site first; a syntax error in functions.php can make the site inaccessible.
<?php
/**
* Fetch remote weather data and cache it for one hour.
*
* @return array|false Decoded JSON response, or false on failure.
*/
function myplugin_get_cached_weather() {
$transient_key = 'myplugin_remote_weather';
$expiration = HOUR_IN_SECONDS;
// 1. Check the cache first.
$cached = get_transient( $transient_key );
if ( false !== $cached ) {
return $cached;
}
// 2. Cache miss: call the remote API.
$response = wp_remote_get( 'https://api.example.com/weather' );
if ( is_wp_error( $response ) ) {
return false;
}
$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body, true );
if ( null === $data || json_last_error() !== JSON_ERROR_NONE ) {
return false;
}
// 3. Store the result for next time.
set_transient( $transient_key, $data, $expiration );
return $data;
}
The key is namespaced with myplugin_ to avoid collisions with other plugins or themes. On the first load after deployment, the function calls the remote API. On later loads within the hour, it returns the cached array.
When no object cache plugin is active, you can confirm storage by inspecting the wp_options table for rows named _transient_myplugin_remote_weather and _transient_timeout_myplugin_remote_weather.
Where transients fall short
- Transients are not suitable for critical state that must survive cache flushes or server migrations.
- Very large values in
wp_optionscan bloat the autoloaded options table and slow down site loads. - Expiration times are not precise; do not use them for security or billing enforcement.
- Object cache backends may evict transients under memory pressure before the set expiration.
- A popular transient expiring at peak traffic can cause a cache stampede, where many requests miss simultaneously and hammer the API. Use a lock or
wp_cache_get()/wp_cache_set()for high-traffic keys.
Check that it is working
- On a local WordPress install, temporarily call
set_transient( 'verify_test', 'hello', 10 ), thenget_transient( 'verify_test' )immediately. - Reload the page; the value should return
hello. - Wait more than ten seconds and reload again; it should return
false. - With no object cache plugin, inspect
wp_optionsfor_transient_verify_testand_transient_timeout_verify_testrows. - For real traffic, enable Query Monitor or a similar debugging plugin to confirm transient hits and misses during page loads.
Rollback
Because this code writes to the database or object cache, you can roll back by removing the snippet from functions.php or your custom plugin. You can also clear the specific value with delete_transient( 'myplugin_remote_weather' ), or flush the object cache if one is active.
Actionable closing
Start with one expensive remote call. Wrap it in get_transient() and set_transient() using a namespaced key and a short expiration. Monitor with Query Monitor, and do not store secrets or financial data in transients unless your object cache is encrypted and access-controlled.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.