API Data in WordPress Themes — a practical guide to WordPress theme external API with clear examples you can reuse in real projects.
WordPress Theme Development Series (58/96). Prefer one article? Read the complete WordPress theme development tutorial.
Short description
Fetch on the server when possible, cache with transients, and never expose secrets in frontend JS.
Transient-cached fetch
$data = get_transient('acme_api_data');
if (false === $data) {
$res = wp_remote_get('https://api.example.com/data', ['timeout' => 10]);
$data = is_wp_error($res) ? [] : json_decode(wp_remote_retrieve_body($res), true);
set_transient('acme_api_data', $data, 15 * MINUTE_IN_SECONDS);
}