Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Mini guide to setting/handling defaults when using arrays with WordPress options
- Covers restoring defaults, safely adding new data, and wiping data.
- Template usage for array options: <?php $options = get_option('muffin_options'); echo $options['logo_text']; ?>
- */
- add_action( 'admin_init', 'muffin_options_init' );
- function muffin_options_init() {
- register_setting( 'muffin_options', 'muffin_options' );
- muffin_options_defaults();
- }
- function muffin_options_defaults() {
- // Get the options
- $muffin_options = get_option('muffin_options');
- // If there are no options, create an empty array to assist with the default options array handling below
- if(empty($muffin_options)) $muffin_options = [];
- $muffin_options_default['logo_text'] = 'Hello World';
- $muffin_options_default['post_style'] = 'excerpts';
- $muffin_options_default['default_featuredimage'] = get_template_directory_uri().'/images/posts/featuredimage-default.png';
- $muffin_options_default['nav_style'] = 'right';
- $muffin_options_default['blog_title'] = 'Latest News';
- /*
- Comment or uncomment the code below. The currently uncommented is generally the only code you'll
- need to use. The other available choices can be very helpful too though.
- */
- # [1] Create 'muffin_options', using the defaults (the data listed above)
- # [?] Basically useless to us - use [4] to set up your data instead
- // add_option('muffin_options', $muffin_options_default);
- # [2] Add any new default data, but don't overwrite anything.
- # [?] Use to add new data while preserving user changes
- $muffin_options = array_merge($muffin_options_default,$muffin_options);
- update_option('muffin_options', $muffin_options);
- # [3] Restore defaults, but KEEP all other data
- # [?] Use to both add new data, and overwrite user changes to default data. Good with a 'Restore Defaults' button
- // $muffin_options = array_merge($muffin_options,$muffin_options_default);
- // update_option('muffin_options', $muffin_options);
- # [4] Restore defaults, and WIPE all other data
- # [?] Use to both replace all option data with defaults, and clear all user data. Use for/to emulate fresh installs
- // update_option('muffin_options', $muffin_options_default);
- # [5] Delete the option entirely
- # [?] Use to remove the option from the database entirely- good for resets, testing and cleanup
- // delete_option('muffin_options');
- }
- // BONUS! This tiny function is a huge help when using options.
- function issetor(&$var, $default=false) {
- return isset($var) ? $var : $default;
- }
- /*
- It's a shortcut to saying "if this variable has a value set, echo that value, otherwise, return false".
- Usage: <?php echo issetor($options['name']); ?>
- You can pass a second argument too. This will be returned if the variable doesn't have a value set.
- Usage: <?php echo issetor($options['name'], 'Joe Bloggs'); ?>
- The function helps prevent errors, and makes your code shorter. Here's how to use it with radio buttons:
- <label>
- <input type='radio' name='muffin_options[post_style]' <?php checked( issetor($options['post_style']),'excerpts' ); ?> value='excerpts'>
- Show Excerpts
- </label> <br />
- <label>
- <input type='radio' name='muffin_options[post_style]' <?php checked( issetor($options['post_style']),'fullposts' ); ?> value='fullposts'>
- Show Full Posts
- </label>
- */
- ?>
Add Comment
Please, Sign In to add comment