Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created January 12, 2017 13:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/1c3af0efb30c5ac8203ec225be6aa8c9 to your computer and use it in GitHub Desktop.
Save tommcfarlin/1c3af0efb30c5ac8203ec225be6aa8c9 to your computer and use it in GitHub Desktop.
[WordPress] Using a WordPress Hook for Updating Options
<?php
/**
* Initializes the defined actions.
*/
public function init() {
add_action(
'update_option',
array( $this, 'toggle_other_option' ),
10, 3
);
}
<?php
/**
* If the given option_name is `user_is_disguised`, then toggles another option
* in the database - `user_is_superman` - based on the specified values.
*
* @param string $option_name The name of the value being updated.
* @param mixed $old_value The previous value of the option before updating it.
* @param mixed $new_value The value of the option now that it's been updated.
*/
public function toggle_other_option( $option_name, $old_value, $new_value ) {
// If 'user_is_disguised' is not being changed, then don't worry about it.
if ( 0 !== strcasecmp( $option_name, 'user_is_disguised' ) ) {
return;
}
/* If 'user_is_disguised' is being set, then remove the Superman value; otherwise,
* set it to true (but don't worry about autoloading it).
*/
if ( '1' === $new_value ) {
delete_option( 'user_is_superman' );
} else {
update_option( 'user_is_superman', true, false );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment