Skip to content

Instantly share code, notes, and snippets.

@westonruter
Created July 24, 2014 14:11
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 westonruter/353a8d3871b7281df857 to your computer and use it in GitHub Desktop.
Save westonruter/353a8d3871b7281df857 to your computer and use it in GitHub Desktop.
<?php
/**
* Safely update an option which may not exist yet, doing add_option() with
* autoload=no if it doesn't. Prevent proliferation of autoloaded options.
*
* Note we check to see if the option exists so we get a chance to
* call add_option with the autoload argument. This is an ugly
* aspect of WordPress. This is a performance consideration, to
* prevent WordPress from needlessly querying these options with
* every request.
* See http://codex.wordpress.org/Function_Reference/update_option
*
* @param $option_name
* @param $option_value
*
* @return bool
*/
function update_option_no_autoload( $option_name, $option_value ) {
$autoload = 'no';
$not_exists = new stdClass();
$existing_option = get_option( $option_name, $not_exists );
if ( $existing_option === $not_exists ) {
return add_option( $option_name, $option_value, null, $autoload );
} else {
return update_option( $option_name, $option_value );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment