1. Options API
Purpose:
- Stores persistent data in the WordPress database (
wp_optionstable). - Data remains until you manually delete or update it.
When to Use:
- Site settings, plugin configurations, feature toggles.
- Data that doesn’t expire automatically.
Example:
// Save option
update_option( 'my_option', 'Hello World' );
// Get option
$value = get_option( 'my_option' );
// Delete option
delete_option( 'my_option' );2. Transients API
Purpose:
- Stores temporary cached data in the database (or memory if object caching is enabled).
- Has a built-in expiration time.
- Automatically deleted when expired.
When to Use:
- Storing API responses, query results, or calculations that don’t need real-time refresh.
- Reducing load by caching expensive operations.
Example:
// Save transient (expires in 1 hour)
set_transient( 'my_transient', 'Hello World', HOUR_IN_SECONDS );
// Get transient
$value = get_transient( 'my_transient' );
// Delete transient
delete_transient( 'my_transient' );Key Differences Table
| Feature | Options API | Transients API |
|---|---|---|
| Persistence | Permanent until deleted | Temporary with expiry time |
| Use Case | Settings/configs | Cached data |
| Performance | Always stored in DB | Can be stored in memory if caching enabled |
| Expiration | No automatic expiry | Automatic expiry |
| Cleanup | Manual | Automatic on expiry |
💡 Quick Memory Tip:
Options API = Filing cabinet 📂 (keeps things until you throw them away)
Transients API = Sticky notes 📝 (expire and get removed automatically)
