options, and transients, and theme mods — oh my!

40
Options, and Transients, and Theme Mods — Oh my! WordCamp St. Louis March 1, 2014

Upload: konstantin-obenland

Post on 06-Dec-2014

1.078 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Options, and Transients, and Theme Mods — Oh my!

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

WordCamp St. Louis March 1, 2014

Page 2: Options, and Transients, and Theme Mods — Oh my!

Konstantin ObenlandChairman Of Height at Automattic

!@obenland

konstantin.obenland.it

Page 3: Options, and Transients, and Theme Mods — Oh my!

get_option() queries the database every time it is called.

True or False?

Page 4: Options, and Transients, and Theme Mods — Oh my!

Transients are Options with an expiration date.

True or False?

Page 5: Options, and Transients, and Theme Mods — Oh my!

wp-includes/option.php

Page 6: Options, and Transients, and Theme Mods — Oh my!

Options vs. TransientsChoose one.

Page 7: Options, and Transients, and Theme Mods — Oh my!

Options API

Page 8: Options, and Transients, and Theme Mods — Oh my!

A simple and way of storing arbitrary data in the database.

Page 9: Options, and Transients, and Theme Mods — Oh my!

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!

What are Notoptions?

Page 11: Options, and Transients, and Theme Mods — Oh my!

Well, they are not options!

Page 12: Options, and Transients, and Theme Mods — Oh my!

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!

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!

Transients API

Page 15: Options, and Transients, and Theme Mods — Oh my!

A simple way of storing cached data.

Page 16: Options, and Transients, and Theme Mods — Oh my!

Transients API ↓

Object Cache API ↓

WP_Object_Cache

Page 17: Options, and Transients, and Theme Mods — Oh my!

They may use the Options API.

Page 18: Options, and Transients, and Theme Mods — Oh my!

They can optionally expire.

Page 19: Options, and Transients, and Theme Mods — Oh my!

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!

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!

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!

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!

External Object Cache

Page 24: Options, and Transients, and Theme Mods — Oh my!

By default, the built-in the object cache is non-persistent.

Page 25: Options, and Transients, and Theme Mods — Oh my!

Pluggable, to allow for third party object caches.

Page 26: Options, and Transients, and Theme Mods — Oh my!

Most popular: APC & Memcached

Source: Grokking the WordPress Object Cache; Tollmanz, Zack.

Page 27: Options, and Transients, and Theme Mods — Oh my!

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!

Theme Mods

Page 29: Options, and Transients, and Theme Mods — Oh my!

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!

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!

Site Options & Site Transients

Page 32: Options, and Transients, and Theme Mods — Oh my!

Uses the sitemeta table on Multisite.

Page 33: Options, and Transients, and Theme Mods — Oh my!

User Settings & User Options

Page 34: Options, and Transients, and Theme Mods — Oh my!

User Settings

Page 35: Options, and Transients, and Theme Mods — Oh my!

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!

User OptionsUses the User Meta API

Page 37: Options, and Transients, and Theme Mods — Oh my!

But!

Page 38: Options, and Transients, and Theme Mods — Oh my!

User Options vs. User Meta

Page 39: Options, and Transients, and Theme Mods — Oh my!

Thank you!

Page 40: Options, and Transients, and Theme Mods — Oh my!

Konstantin Obenland@obenland

konstantin.obenland.it