Options, and Transients,
and Theme Mods — Oh my!
WordCamp St. Louis
March 1, 2014
Konstantin Obenland
Chairman 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. Transients
Choose one.
Options API
A simple and way of storing
arbitrary data in the database.
get_option()
Is it in $all_options?
Yes

No
Is it in $wp_cache?

Return value.

Yes

No
Is it in the database?

Yes
No

Add to $wp_cache

Return $default.
What are Notoptions?
Well, they are not options!
get_option()
Is it in $all_options?
Yes

No
Is it in $wp_cache?

Return value.

Yes

No
Is it in the database?

Yes
No

Add to $wp_cache

Return $default.
get_option()
Is it in $notoptions?

Yes

No
Is it in $all_options?

Yes

No
Is it in $wp_cache?

Return value.

Yes

No
Is it in the database?

Yes
No

Add to $wp_cache

Add to $notopions

Return $default.
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 Options
Uses the User Meta API
But!
User Options vs. User Meta
Thank you!
Konstantin Obenland
@obenland
konstantin.obenland.it

Options, and Transients, and Theme Mods — Oh my!