Caching
How the weather is fetched
The weather is fetched in the background, by WP-Cron, never while a visitor waits:
- Every five minutes WP-Cron checks what is due. The current weather is fetched every Refresh every minutes (Settings, Weather data; 10 by default, 5 to 60). IPMA's station measurements are fetched at most every 15 minutes, since stations report once an hour; the forecasts and the sea once an hour.
- Pages show the stored copy. A page that finds the weather overdue asks WP-Cron to run, in a separate request, and does not wait for it.
- Each request to a weather service gives up after 5 seconds, and a refresh never runs twice at the same time.
- When a service fails, the last good data stays on the site, and that part is not asked for again for five minutes. The Status card on the Settings tab shows what failed and when.
- Refresh now on the Status card fetches everything at once. Saving a new source, key or list of towns does the same, so you see straight away whether it works.
Before the very first refresh, widgets show a light placeholder that fills in as soon as the weather is there.
Page caches and CDNs
Madeira Weather works with page caching plugins (WP Rocket, LiteSpeed Cache, SWIS, W3 Total Cache...) and CDNs, with nothing to exclude:
- The page carries the weather of when it was cached. As soon as it loads, the browser asks the site when the weather last changed (
/stamp) and, if the page is older, asks for its widgets drawn again (/render) and swaps them in place: the cards, the map, the forecast, the sun, the comparison, the new widgets and the single values. The looks need nothing new: they are plain CSS on the page. - Those requests (
/wp-json/madeira-weather/v1/...) are public reads with no security token, so a page cached for days still works. They send no cookies. - Their answers are the same for every visitor and say so:
Cache-Control: public, max-age=60for/stamp, 120 for/renderand the current weather, longer for the forecast and the sun. A CDN may keep them that long. - If those requests fail, the cached weather simply stays on the page.
The [mw_loop] template is printed with the page only; on a cached page it shows the weather of when the page was cached. Clear the page cache after changing settings or the look, as for any plugin.
When WP-Cron does not run
Some hosts block the requests WP-Cron makes to the site itself. The plugin then refreshes when a visitor's browser asks for the weather and finds it more than about half an hour old (one browser at a time, under the same rules). The weather stays current on busy pages, but the Status card will show long gaps.
If your site sets DISABLE_WP_CRON, the Status card says so: make sure a server cron job calls wp-cron.php every few minutes.
The REST routes
| Route | Answers |
|---|---|
GET /wp-json/madeira-weather/v1/weather | Current weather of every town shown, with sunrise and sunset, the sea, and the data source |
GET /wp-json/madeira-weather/v1/forecast?location=santana&days=5 | One town's forecast. A name that is not one of your towns answers 404. |
GET /wp-json/madeira-weather/v1/sun-times | Today's sunrise and sunset |
GET /wp-json/madeira-weather/v1/sea-temperature | Sea temperature and wave height off Funchal |
GET /wp-json/madeira-weather/v1/stamp | When the stored weather last changed (a Unix time) |
GET /wp-json/madeira-weather/v1/render?w=[["madeira_weather_cards",{"locations":"Funchal"}]] | Up to 24 of the plugin's widgets, drawn from the stored weather, as HTML. Only the plugin's own shortcodes. |
Before the first refresh they answer {"pending": true}. With a persistent object cache (Redis, Memcached), one address may make 600 requests an hour; the madeira_weather_rate_limit filter changes that (0 turns it off), and behind a proxy or Cloudflare the madeira_weather_client_ip filter tells the plugin the visitor's real address.
Developers
madeira_weather_refreshed(action) runs after each refresh, with the stored data and the parts fetched.madeira_weather_compare_data(filter) lets you change the comparison table's values.madeira_weather_before_cardsandmadeira_weather_after_cards(actions) run around each card grid.madeira_weather_is_night(filter) decides day or night icons for the current request.madeira_weather_sourcesandmadeira_weather_regions(filters) register a data source (Madeira_Weather_Source) or a region pack (Madeira_Weather_Region): the core knows nothing about Madeira, which is its first region pack. There is no setting for another region yet.