Slide 1

Slide 1 text

WordPress Junk Drawer and how to organize it.

Slide 2

Slide 2 text

option_id option_name option_value autoload 1 site_url http://mywebsite.com yes 2 blogname My Website yes

Slide 3

Slide 3 text

option_id option_name option_value autoload 1 site_url http://mywebsite.com yes 2 blogname My Website yes varchar(65) This means you can store up to 65 characters in this field to use as an identifier. longtext longtext can store up to 4,294,967,295 or 4GB (232 – 1) of data. This is far more than the whole of WordPress core(6.1 MB zipped / 17.2 MB extracted).

Slide 4

Slide 4 text

DATA Simplest way to identify data Key => Value $key = “value”;

Slide 5

Slide 5 text

Storing data long term - Defaults - Configurations - Version - Install dates - Fallbacks

Slide 6

Slide 6 text

update_option( ‘key’, ‘value’ ); get_option( ‘key’ ); string(5) "value" OR false option_id option_name option_value autoload 2400 key value yes

Slide 7

Slide 7 text

Storing Data Short Term - User entered values (frontend, preferences) - API requests - Cart items - Anything you can put an expiration on

Slide 8

Slide 8 text

Time in WP MINUTE_IN_SECONDS HOUR_IN_SECONDS DAY_IN_SECONDS WEEK_IN_SECONDS YEAR_IN_SECONDS https://codex.wordpress.org/Easier_Expression_of_Time_Constants

Slide 9

Slide 9 text

set_transient( ‘key’, ‘value’, DAY_IN_SECONDS ); get_transient( ‘key’ ); string(5) "value" OR false option_id option_name option_value autoload 2401 _transient_timeout_key 1390595410 no 2402 _transient_key value no

Slide 10

Slide 10 text

Theme Mods set_theme_mod( ‘theme_key’, ‘value’ ); get_theme_mod( ‘theme_key’ ); string(5) "value" OR false option_id option_name option_value autoload 2403 theme_mods_themeslug a:1:{s:9:"theme_key";s:5:"value";} yes

Slide 11

Slide 11 text

Strength in built-in functions _option(...) : simple query for key returns value _transient(...) : short term storage (expires) _theme_mod(...) : organized data(single row) for specific theme

Slide 12

Slide 12 text

Organizing YOUR code $my_data = array( ‘format’ => ‘list’, ‘display’ => ‘all’, ... ); update_option( ‘my_master_key’, $my_data ); option_id option_key option_value autoload 2658 my_key a:2:{s:6:"format";s:4:"list";s:7:" display";s:3:"all";} yes

Slide 13

Slide 13 text

Retrieving Organized Code $my_settings = get_option( ‘my_master_key’ ); $format = $my_setting[‘format’]; OR ... option_id option_key option_value autoload 2658 my_key a:2:{s:6:"format";s:4:"list";s:7:" display";s:3:"all";} yes

Slide 14

Slide 14 text

Helper Functions - Update function {prefix}_update_option( $key, $value ) { $settings = get_option( ‘my_master_key’ ); $settings[ $key ] = $value; return update_option( ‘my_master_key’, $settings ); }

Slide 15

Slide 15 text

Helper Functions - Get function {prefix}_get_option( $key, $value ) { $settings = get_option(‘my_master_key’); return $settings[ $key ]; }

Slide 16

Slide 16 text

Organizing Transients - No update_ function - Must set_ it like it is the first time - Must set an expiration

Slide 17

Slide 17 text

Sneaky Transient Loop $t = get_transient(‘group_key’); if( ! isset( $t[‘key’] ) ) … $t[‘key’] = “some new info”; set_transient( ‘group_key’, $t, DAY_IN_SECONDS);

Slide 18

Slide 18 text

Force Transient Removal Options - remove it on admin login or some other hook (could void the use of a transient altogether) - wp_cron to remove it every DAY_IN_SECONDS (this makes some transients less than 24h but still efficient)

Slide 19

Slide 19 text

Advanced Uses - Multisite get_transient() ---> get_site_transient() set_transient() ---> set_site_transient() set_option() ---> set_site_option() get_option() ---> get_site_option() update_option() ---> update_site_option() delete_option() ---> delete_site_option()

Slide 20

Slide 20 text

set_site_transient( ‘key’, ‘value’, DAY_IN_SECONDS ); get_site_transient( ‘key’ ); string(5) "value" OR false option_id option_name option_value autoload 2401 _site_transient_timeout_key 1390595410 yes 2402 _site_transient_key value yes

Slide 21

Slide 21 text

When not to use these functions When the info is specific to a registered user (user_meta) When the info is specific to an individual post (post_meta) meta_id post_id meta_key meta_value 1 24 key value meta_id user_id meta_key meta_value 1 3 key value

Slide 22

Slide 22 text

Clean up on uninstall delete_option(‘your_key’); One function to remove one row instead of multiple(possibly thousands) based only on a prefix or a long list of individual keys.

Slide 23

Slide 23 text

@MikeHansenMe MikeHansen.Me github.com/MikeHansenMe bluehost.com/wordcamp for special pricing Questions?