I’m building a simple WordPress plugin that fetches a random quote from an external API and displays it using a shortcode.
The problem is: every time I refresh the page, the quote seems to duplicate — it appears twice (or more) inside the same container. I expected it to show just one quote per page load.
Here’s a simplified version of my code:
function my_quotes_shortcode() {
$response = wp_remote_get('https://api.quotable.io/random');
if (is_wp_error($response)) {
return 'Quote unavailable.';
}
$data = json_decode(wp_remote_retrieve_body($response));
$quote = $data->content ?? 'Default quote';
$author = $data->author ?? 'Unknown';
return "<div class='quote-box'>"{$quote}" - {$author}</div>";
}
add_shortcode('daily_quote', 'my_quotes_shortcode');
What I Tried
- Wrapped the return inside ob_start() / ob_get_clean() — no change
- Ensured add_shortcode() is called only once, outside any hook
- Cleared browser, site, and server cache
- Switched to the default Twenty Twenty-Four theme — still duplicates
What I Expected
- I expected the shortcode to return just one quote inside a each time the page loads. No duplicates.
Notes
- I’m not enqueueing any custom JavaScript or CSS
- Using the shortcode inside a basic Page block through the WordPress editor
- Duplicates happen more often when refreshing quickly
Is this related to how WordPress handles shortcode rendering or something with API timing?
Any help would be appreciated!