Skip to content

Instantly share code, notes, and snippets.

@ithinkandicode
Last active November 12, 2018 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ithinkandicode/10ee890dd6f91354e3a52af2c8a082c2 to your computer and use it in GitHub Desktop.
Save ithinkandicode/10ee890dd6f91354e3a52af2c8a082c2 to your computer and use it in GitHub Desktop.
Manage WP options
<?php
/**
* WP Options Manager
*
* Description:
* Useful ways to set/manage defaults when using arrays with WordPress options.
* Covers restoring defaults, safely adding new data, and wiping data.
* Prefix:
* itaic_
* Usage within templates:
* $options = get_option('itaic_options');
* echo $options['logo_text'];
* echo $options['post_style'];
*
*/
add_action( 'admin_init', 'itaic_options_init' );
function itaic_options_init()
{
// Registers your settings. Only required on first use, to add data to the DB, but there's no harm
// in keeping it here
register_setting( 'itaic_options', 'itaic_options' );
// Run the the defaults handler function below
itaic_options_defaults();
}
function itaic_options_defaults()
{
// Get the options from the DB
$itaic_options = get_option('itaic_options');
// If there are no options, create an empty array to assist with the default options array handling below
if(empty($itaic_options)) $itaic_options = [];
$itaic_options_default['logo_text'] = 'Hello World';
$itaic_options_default['post_style'] = 'excerpts';
$itaic_options_default['default_featuredimage'] = get_template_directory_uri().'/images/posts/featuredimage-default.png';
$itaic_options_default['nav_style'] = 'right';
$itaic_options_default['blog_title'] = 'Latest News';
/*
Comment or uncomment the code below.
Block 2 is generally the only code you'll need to use.
The other available choices can be very helpful too though.
*/
# 1 Create 'itaic_options', using the defaults (the data listed above)
# ? Basically useless to us - use [4] to set up your data instead
// add_option('itaic_options', $itaic_options_default);
# 2 Add any new default data, but don't overwrite anything.
# ? Use to add new data while preserving user changes
$itaic_options = array_merge($itaic_options_default, $itaic_options);
update_option('itaic_options', $itaic_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
// $itaic_options = array_merge($itaic_options,$itaic_options_default);
// update_option('itaic_options', $itaic_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('itaic_options', $itaic_options_default);
# 5 Delete the option entirely
# ? Use to remove the option from the database entirely- good for resets, testing and cleanup
// delete_option('itaic_options');
}
// BONUS! This tiny function is a huge help when using options.
/**
* Returns the value of $var if it's been set.
* Otherwise, returns the if_unset value (defaults to false).
*
* Example:
* echo issetor($options['name']);
* echo issetor($options['name'], 'Joe Bloggs');
*
* @param mixed $var Var to check if it's been set or not
* @param boolean $if_unset Value to return if $var is unset (default: false)
* @param boolean $echo If true, echo the result
*
* @return mixed Returns the value if it's been set, or the value of if_unset if unset (default: false)
*/
function issetor(&$var, $if_unset=false, $echo=false)
{
$output = isset($var) ? $var : $if_unset;
if($echo)
{
echo $output;
}
else
{
return $output;
}
}
/*
Example usage with radio buttons:
<label>
<input type='radio' name='itaic_options[post_style]' <?php checked( issetor($options['post_style']),'excerpts' ); ?> value='excerpts'>
Show Excerpts
</label>
<label>
<input type='radio' name='itaic_options[post_style]' <?php checked( issetor($options['post_style']),'fullposts' ); ?> value='fullposts'>
Show Full Posts
</label>
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment