options, and transients, and theme mods — oh my!
DESCRIPTION
TRANSCRIPT
Options, and Transients, and Theme Mods — Oh my!
WordCamp St. Louis March 1, 2014
Konstantin ObenlandChairman Of Height at Automattic
!@obenland
konstantin.obenland.it
get_option() queries the database every time it is called.
True or False?
Transients are Options with an expiration date.
True or False?
wp-includes/option.php
Options vs. TransientsChoose one.
Options API
A simple and way of storing arbitrary data in the database.
get_option()
Is it in $all_options?
Is it in $wp_cache?
Is it in the database? Add to $wp_cache
Return value.
Return $default.
No
No
No
Yes
Yes
Yes
What are Notoptions?
Well, they are not options!
Is it in $all_options?
Is it in $wp_cache?
Is it in the database? Add to $wp_cache
Return value.
Return $default.
No
No
No
Yes
Yes
Yes
get_option()
Is it in $all_options?
Is it in $wp_cache?
Is it in the database? Add to $wp_cache
Return value.
Return $default.
No
No
No
Yes
Yes
Yes
Is it in $notoptions?
NoYes
Add to $notopions
get_option()
Transients API
A simple way of storing cached data.
Transients API ↓
Object Cache API ↓
WP_Object_Cache
They may use the Options API.
They can optionally expire.
function get_transient( $transient ) { !
if ( wp_using_ext_object_cache() ) { return wp_cache_get( $transient, 'transient' ); !
} else { // Check wp_load_all_options() !
return get_option( $transient_option ); } }
function set_transient( $transient, $value, $expiration = 0 ) { // Check for external object cache and use it. // Check DB for existing value and update expiration. // Add an option with the expiration of this transient. // Add the transient value to the DB. }
function get_my_data() { $data = get_transient( 'my_data' ); if ( false === $data ) { $data = $wpdb->get_results( $query ); set_transient( 'my_data', $data, DAY_IN_SECONDS ); } !
// Do something with $data. !
return $data; }
function get_my_data() { $data = wp_cache_get( 'my_data', 'my_group' ); if ( false === $data ) { $data = $wpdb->get_results( $query ); wp_cache_set( 'my_data', $data, 'my_group', DAY_IN_SECONDS ); } !
// Do something with $data. !
return $data; }
External Object Cache
By default, the built-in the object cache is non-persistent.
Pluggable, to allow for third party object caches.
Most popular: APC & Memcached
Source: Grokking the WordPress Object Cache; Tollmanz, Zack.
Recommended Reading
http://vip.wordpress.com/documentation/caching/
http://tollmanz.com/core-caching-concepts-in-wordpress/
http://tollmanz.com/grokking-the-wp-object-cache/
http://scotty-t.com/2012/01/20/wordpress-memcached/
Theme Mods
set_theme_mod( $name, $value ); get_theme_mod( $name, $default = false ); remove_theme_mod( $name ); !
get_theme_mods(); remove_theme_mods();
function get_theme_mod( $name, $default = false ) { $mods = get_option( 'theme_mods_' . get_option( 'stylesheet' ) ); !
if ( isset( $mods[ $name ] ) ) return $mods[ $name ]; !
return $default; }
Site Options & Site Transients
Uses the sitemeta table on Multisite.
User Settings & User Options
User Settings
User Settings
Primary examples are the expansion/collapse of the admin menu, and switching between text and visual. These can happen without a form submit, and you wouldn't want to fire an XHR request every time just to save the previous state.
—Andrew Nacin
User OptionsUses the User Meta API
But!
User Options vs. User Meta
Thank you!