Action
uncanny-automator
automator_update_option_{$key}
Fires after an option with a specific key is updated, passing the key and the new value.
add_action( 'automator_update_option_{$key}', $callback, 10, 2 );
Description
Fires after an option is successfully updated in the database. Developers can use this hook to perform actions based on specific option changes, like invalidating related caches or triggering further logic. The hook is dynamically named after the option key being updated.
Usage
add_action( 'automator_update_option_{$key}', 'your_function_name', 10, 2 );
Parameters
-
$key(mixed) - The `$key` parameter represents the unique identifier of the option being updated.
-
$value(mixed) - The `$key` parameter is the identifier for the option being updated.
Examples
add_action( 'automator_update_option_automator_settings', 'my_automator_settings_updated', 10, 2 );
/**
* Logs when the automator settings option is updated.
*
* This function serves as an example of how to hook into the
* `automator_update_option_{$key}` action. In this case, it's
* specifically targeting the 'automator_settings' option.
*
* @param string $key The option key that was updated.
* @param mixed $value The new value of the option.
*/
function my_automator_settings_updated( $key, $value ) {
// In a real-world scenario, you might log this change,
// send a notification, or perform other administrative tasks.
// For demonstration, we'll just log it to the debug log.
if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
error_log( "Automator settings updated. Key: {$key}, New Value: " . print_r( $value, true ) );
}
}
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/core/classes/class-automator-options.php:391
public function update_option( string $key, $value, bool $autoload = true ) {
$key = $this->validate_key( $key );
if ( false === $key ) {
return false;
}
// Check if option exists and value is unchanged.
$exists = $this->query->option_exists( $key );
if ( true === $exists ) {
$current = $this->get_option( $key );
if ( $current === $value ) {
// Value unchanged - this is a successful no-op
return true;
}
}
$encoded_value = Automator_Option_Formatter::encode_value( $value );
$serialized_value = maybe_serialize( $encoded_value );
// Upsert to database.
if ( false === $this->query->upsert_option( $key, $serialized_value, $autoload ) ) {
return false;
}
// Delete existing cache entries.
$this->cache->delete( $key );
// Update cache with new value.
$this->cache->set( $key, $value, $serialized_value, $autoload );
do_action( "automator_update_option_{$key}", $key, $value );
do_action( 'automator_updated_option', $key, $value );
return true;
}