Upgrade to Pro — share decks privately, control downloads, hide ads and more …

WordPress Junk Drawer - and how to organize it

Mike Hansen
February 27, 2014

WordPress Junk Drawer - and how to organize it

In this presentation we will cover some built in WordPress functions like _option, _transient, and _theme_mod. We will discuss when to use each and how you may better organize your code.

Mike Hansen

February 27, 2014
Tweet

More Decks by Mike Hansen

Other Decks in Technology

Transcript

  1. 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).
  2. update_option( ‘key’, ‘value’ ); get_option( ‘key’ ); string(5) "value" OR

    false option_id option_name option_value autoload 2400 key value yes
  3. Storing Data Short Term - User entered values (frontend, preferences)

    - API requests - Cart items - Anything you can put an expiration on
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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 ); }
  10. Helper Functions - Get function {prefix}_get_option( $key, $value ) {

    $settings = get_option(‘my_master_key’); return $settings[ $key ]; }
  11. Organizing Transients - No update_ function - Must set_ it

    like it is the first time - Must set an expiration
  12. 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);
  13. 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)
  14. 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()
  15. 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
  16. 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
  17. 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.