-
-
Save tommcfarlin/1c3af0efb30c5ac8203ec225be6aa8c9 to your computer and use it in GitHub Desktop.
[WordPress] Using a WordPress Hook for Updating Options
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Initializes the defined actions. | |
*/ | |
public function init() { | |
add_action( | |
'update_option', | |
array( $this, 'toggle_other_option' ), | |
10, 3 | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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