WordPress Core APIs
Overview:
WordPress Core APIs provide functions to extend WordPress safely. They handle heavy-lifting tasks and ensure backward compatibility for plugins and themes.
Core APIs Learned:
Metadata API – manage metadata for posts, users, comments, terms
Shortcode API – create dynamic shortcodes
Admin Menus – add menus in admin dashboard
Settings & Options API – manage plugin/theme settings
Advanced Core APIs:
HTTP API – remote requests with caching & authentication
Transients API – temporary caching
Rewrite API – custom URLs/permalinks
Dashboard & Widgets API – add admin/dashboard widgets
Filesystem API – secure file operations
File Header API – plugin/theme metadata
Abilities API – manage user capabilities
Transient API – Overview:
Temporary caching stored in DB (
wp_options) or object cacheSupports expiration; reduces expensive queries or API calls
Stored with prefix
_transient_and_transient_timeout_
Key Functions & Examples:
// Set a transient (expires in 1 hour)
set_transient('my_transient', 'Hello World', 3600);
// Get a transient
$value = get_transient('my_transient');
if(false === $value){
$value = 'Regenerated';
set_transient('my_transient', $value, 3600);
}
// Delete a transient
delete_transient('my_transient');
// Cache expensive queries
$recent_posts = get_transient('recent_posts');
if(false === $recent_posts){
$recent_posts = wp_get_recent_posts(['numberposts'=>5,'post_status'=>'publish']);
set_transient('recent_posts', $recent_posts, 12*HOUR_IN_SECONDS);
}
Best Practices:
Use for temporary, non-critical data
Check existence before regenerating
Delete manually if needed
Avoid storing sensitive data
Important Notes:
Transients are auto-deleted on expiration or manually
Expiration time stored in
_transient_timeout_{name}Keys must be alphanumeric, <172 chars