Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin

tommcfarlin/00-init.php Secret

Created Jan 12, 2017
Embed
What would you like to do?
[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