Tiny class for WordPress plugins that stores transient data that's post specific in the post meta.
WordPress transients are great for storing data that's, well, transient. API call results, that sort of thing. But I've found that most of the time I'm storing transient data it's specific to a post. In the past what I've done is to include the post id in the transient name. And while that works, it just feels messy.
What this class does is store transient data in the post meta table along with the expiration so that it's invalidated when that expiration passes.
For now you'll need to include the file somewhere in your plugin or theme and either include or require it.
I'd like to make it a composer library at some point, but haven't found a good way to keep the dependency from overlapping if multiple plugins use this class.
The class definition is wrapped in a if class_exists so it should play nice if there are other plugins in your installation using the same class.
use Aelora\WordPress\PostTransient;
PostTransient::set($postID, $key, $value, $expiration);| Parameter | Description |
|---|---|
| $postID | The id for the post to store transient. This will probably be $post->ID in most cases. |
| $key | Identifier for the transient. Needs to be unique per post. _transient_ is appended to the key name before storing in the database. |
| $value | The value to store. This can be either a simple piece of data or a structure like an array or object. |
| $expiration | (optional) Number of seconds before the transient expires. If you leave this off it defaults to 24 hours. |
use Aelora\WordPress\PostTransient;
$x = PostTransient::get($postID, $key, $default);| Parameter | Description |
|---|---|
| $postID | The id for the post to retrieve the transient. |
| $key | Identifier for the transient to retrieve. |
| $default | Value to return if the transient has expired or is not found. Defaults to an empty string. |
use Aelora\WordPress\PostTransient;
PostTransient::delete($postID, $key);| Parameter | Description |
|---|---|
| $postID | The id for the post to remove the transient |
| $key | The key to remove |
If the key does not exist this method silently does nothing.