Options, and Transients, and Theme Mods — Oh my!
-
Upload
konstantin-obenland -
Category
Technology
-
view
1.076 -
download
0
description
Transcript of Options, and Transients, and Theme Mods — Oh my!
![Page 1: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/1.jpg)
Options, and Transients, and Theme Mods — Oh my!
WordCamp St. Louis March 1, 2014
![Page 2: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/2.jpg)
Konstantin ObenlandChairman Of Height at Automattic
!@obenland
konstantin.obenland.it
![Page 3: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/3.jpg)
get_option() queries the database every time it is called.
True or False?
![Page 4: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/4.jpg)
Transients are Options with an expiration date.
True or False?
![Page 5: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/5.jpg)
wp-includes/option.php
![Page 6: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/6.jpg)
Options vs. TransientsChoose one.
![Page 7: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/7.jpg)
Options API
![Page 8: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/8.jpg)
A simple and way of storing arbitrary data in the database.
![Page 9: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/9.jpg)
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
![Page 10: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/10.jpg)
What are Notoptions?
![Page 11: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/11.jpg)
Well, they are not options!
![Page 12: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/12.jpg)
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()
![Page 13: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/13.jpg)
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()
![Page 14: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/14.jpg)
Transients API
![Page 15: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/15.jpg)
A simple way of storing cached data.
![Page 16: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/16.jpg)
Transients API ↓
Object Cache API ↓
WP_Object_Cache
![Page 17: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/17.jpg)
They may use the Options API.
![Page 18: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/18.jpg)
They can optionally expire.
![Page 19: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/19.jpg)
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 ); } }
![Page 20: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/20.jpg)
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. }
![Page 21: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/21.jpg)
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; }
![Page 22: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/22.jpg)
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; }
![Page 23: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/23.jpg)
External Object Cache
![Page 24: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/24.jpg)
By default, the built-in the object cache is non-persistent.
![Page 25: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/25.jpg)
Pluggable, to allow for third party object caches.
![Page 26: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/26.jpg)
Most popular: APC & Memcached
Source: Grokking the WordPress Object Cache; Tollmanz, Zack.
![Page 27: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/27.jpg)
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/
![Page 28: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/28.jpg)
Theme Mods
![Page 29: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/29.jpg)
set_theme_mod( $name, $value ); get_theme_mod( $name, $default = false ); remove_theme_mod( $name ); !
get_theme_mods(); remove_theme_mods();
![Page 30: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/30.jpg)
function get_theme_mod( $name, $default = false ) { $mods = get_option( 'theme_mods_' . get_option( 'stylesheet' ) ); !
if ( isset( $mods[ $name ] ) ) return $mods[ $name ]; !
return $default; }
![Page 31: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/31.jpg)
Site Options & Site Transients
![Page 32: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/32.jpg)
Uses the sitemeta table on Multisite.
![Page 33: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/33.jpg)
User Settings & User Options
![Page 34: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/34.jpg)
User Settings
![Page 35: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/35.jpg)
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
![Page 36: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/36.jpg)
User OptionsUses the User Meta API
![Page 37: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/37.jpg)
But!
![Page 38: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/38.jpg)
User Options vs. User Meta
![Page 39: Options, and Transients, and Theme Mods — Oh my!](https://reader034.fdocuments.us/reader034/viewer/2022051817/5484383c5906b58a158b46b6/html5/thumbnails/39.jpg)
Thank you!