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

1,802 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,802
On SlideShare
0
From Embeds
0
Number of Embeds
27
Actions
Shares
0
Downloads
5
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

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

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

×