0

Let's say I have access to a remote API that stores car data, and I want to be able to place car information box(es) into posts using a shortcode. The shortcode fetches JSON data with wp_remote_get() and server-side renders the HTML using the fetched data.

It seems using the transients API would make this performant, as performing multiple API calls on page load would be slow. Because the data changes infrequently, the expiration time would be set to a day. For the transient key, I would use a unique car ID or perhaps the remote API endpoint.

However, there are times you want the data to be updated immediately, and not wait for the transient to expire. So I want to add a function on the admin dashboard that lets users delete transients.

What would be a good way to manage transients, e.g. delete a single or all transients related to my plugin? It seems I need to keep a separate list of transients I have created somewhere, maybe using the options API. Is there any recommended approach for managing transients with dynamically generated names?

4
  • How are you generating these transient names? There's no code in your question to work with. Normally you would solve this problem by never needing to be in this specific situation in the first place, you need a way to list all the data you have fetched to implement what you want, but by using only transients you've eliminated that as an option Commented Jan 2, 2022 at 21:08
  • Each shortcode would have a car ID as an attribute, which is the unique ID we use to specify the car we fetch from the remote DB when we make the API call. The transient name could therefore simply be {pluginname}-{car_id}. Transients do seem a bit hacky, but the only alternatives I could think of were to instead fetch and write car data to a custom table in WordPress (a lot of extra work to do what transients already basically achieve) or do client-side JS rendering (SEO drawbacks). Commented Jan 2, 2022 at 23:16
  • transients were never intended to be something you can list sort search and filter, they are just caches, and nothing more. To be able to list them you would need a list of cars. Do you have control of the external database/API? Commented Jan 3, 2022 at 0:02
  • It sounds like this is a proposed solution to a problem that you don't know how to implement, and that you've fallen into the classic X Y problem fallacy. Would it not make more sense to ask how to solve your problem instead? AKA how do you clear a cache for a specific car? If you had asked that Q someone could write an answer suggesting a box you enter a car ID into with a "refresh" or "clear cache" button, which would solve your problem, but unfortunately you did not ask that question :( Commented Jan 3, 2022 at 0:04

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.