codemuffin

WordPress: Handling default data with option arrays

Jun 21st, 2015
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.36 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.     Mini guide to setting/handling defaults when using arrays with WordPress options
  5.     Covers restoring defaults, safely adding new data, and wiping data.
  6.  
  7.     Template usage for array options: <?php $options = get_option('muffin_options'); echo $options['logo_text']; ?>
  8. */
  9.  
  10. add_action( 'admin_init', 'muffin_options_init' );
  11.  
  12. function muffin_options_init() {
  13.     register_setting( 'muffin_options', 'muffin_options' );
  14.     muffin_options_defaults();
  15. }
  16.  
  17. function muffin_options_defaults() {
  18.  
  19.     // Get the options
  20.     $muffin_options = get_option('muffin_options');
  21.  
  22.     // If there are no options, create an empty array to assist with the default options array handling below
  23.     if(empty($muffin_options)) $muffin_options = [];
  24.    
  25.     $muffin_options_default['logo_text'] = 'Hello World';
  26.     $muffin_options_default['post_style'] = 'excerpts';
  27.     $muffin_options_default['default_featuredimage'] = get_template_directory_uri().'/images/posts/featuredimage-default.png';
  28.     $muffin_options_default['nav_style'] = 'right';
  29.     $muffin_options_default['blog_title'] = 'Latest News';
  30.  
  31.     /*
  32.         Comment or uncomment the code below. The currently uncommented is generally the only code you'll
  33.         need to use. The other available choices can be very helpful too though.
  34.     */
  35.  
  36.     # [1] Create 'muffin_options', using the defaults (the data listed above)
  37.     # [?] Basically useless to us - use [4] to set up your data instead
  38.     // add_option('muffin_options', $muffin_options_default);
  39.  
  40.     # [2] Add any new default data, but don't overwrite anything.
  41.     # [?] Use to add new data while preserving user changes
  42.     $muffin_options = array_merge($muffin_options_default,$muffin_options);
  43.     update_option('muffin_options', $muffin_options);
  44.  
  45.     # [3] Restore defaults, but KEEP all other data
  46.     # [?] Use to both add new data, and overwrite user changes to default data. Good with a 'Restore Defaults' button
  47.     // $muffin_options = array_merge($muffin_options,$muffin_options_default);
  48.     // update_option('muffin_options', $muffin_options);
  49.  
  50.     # [4] Restore defaults, and WIPE all other data
  51.     # [?] Use to both replace all option data with defaults, and clear all user data. Use for/to emulate fresh installs
  52.     // update_option('muffin_options', $muffin_options_default);
  53.  
  54.     # [5] Delete the option entirely
  55.     # [?] Use to remove the option from the  database entirely- good for resets, testing and cleanup
  56.     // delete_option('muffin_options');
  57. }
  58.  
  59.  
  60.  
  61.  
  62. //  BONUS! This tiny function is a huge help when using options.
  63.  
  64. function issetor(&$var, $default=false) {
  65.     return isset($var) ? $var : $default;
  66. }
  67.  
  68. /* 
  69.     It's a shortcut to saying "if this variable has a value set, echo that value, otherwise, return false".
  70.  
  71.         Usage: <?php echo issetor($options['name']); ?>
  72.  
  73.     You can pass a second argument too. This will be returned if the variable doesn't have a value set.
  74.  
  75.         Usage: <?php echo issetor($options['name'], 'Joe Bloggs'); ?>
  76.    
  77.     The function helps prevent errors, and makes your code shorter. Here's how to use it with radio buttons:
  78.  
  79.         <label>
  80.             <input type='radio' name='muffin_options[post_style]' <?php checked( issetor($options['post_style']),'excerpts' ); ?> value='excerpts'>
  81.             Show Excerpts
  82.         </label> <br />
  83.         <label>
  84.             <input type='radio' name='muffin_options[post_style]' <?php checked( issetor($options['post_style']),'fullposts' ); ?> value='fullposts'>
  85.             Show Full Posts
  86.         </label>
  87.  
  88. */
  89.  
  90. ?>
Add Comment
Please, Sign In to add comment