Scheduling plugin data updates using WP-Cron
When working with external data sources, it's always important to consider the frequency and mechanism used to update this data, as well as caching data locally. While our code in the Optimizing plugin performance by storing external data using transients recipe is functional, it only checks whether the transient data is expired when users visit the shortcode. This slows down the page display if new data has to be fetched and processed. A better approach is to schedule our data update function to be periodically executed when any part of the site is visited, or even on a very specific schedule using WP-Cron.
Getting ready
You should have already followed the Optimizing plugin performance by storing external data using transients recipe to have a starting point for this recipe. Alternatively, you can get the resulting code (ch11/ch11-transit-feed/ch11-transit-feed-v1.php
) from the book's GitHub page and rename the file as...