Action
uncanny-automator
automator_add_option_{$key}
Fires when an option is added, providing access to the option's key and value for further customization.
add_action( 'automator_add_option_{$key}', $callback, 10, 2 );
Description
Fires after an option has been added or updated in the Automator plugin's core options, specifically after the cache has been refreshed. Developers can use this hook to perform custom actions or modifications related to the updated option, identified by its `$key` and `$value`.
Usage
add_action( 'automator_add_option_{$key}', 'your_function_name', 10, 2 );
Parameters
-
$key(mixed) - This parameter contains the unique identifier or name of the option being added or modified.
-
$value(mixed) - This parameter represents the unique identifier or name of the option being added.
Examples
<?php
/**
* Example function to handle the 'automator_add_option_my_custom_setting' action hook.
* This function will be triggered whenever the option with the key 'my_custom_setting' is added or updated.
* It could be used to perform additional logic, like validating the new value,
* updating other related settings, or logging the change.
*/
add_action( 'automator_add_option_my_custom_setting', function ( $key, $value ) {
// Log the update to a custom log file or a WordPress option for auditing.
// In a real scenario, you might want to sanitize and validate the $value here.
error_log( sprintf( 'Automator: Custom setting "%s" updated to: %s', $key, print_r( $value, true ) ) );
// If the new value is an array, you might want to process its elements.
if ( is_array( $value ) && isset( $value['feature_enabled'] ) && true === $value['feature_enabled'] ) {
// Perform an action when a specific feature is enabled via the custom setting.
update_option( 'my_custom_feature_status', 'enabled' );
} elseif ( is_array( $value ) && isset( $value['feature_enabled'] ) && false === $value['feature_enabled'] ) {
// Perform an action when the specific feature is disabled.
update_option( 'my_custom_feature_status', 'disabled' );
}
}, 10, 2 ); // Priority 10, accepts 2 arguments ($key, $value)
/**
* Example function to handle the general 'automator_option_added' action hook.
* This function will be triggered for any option added or updated by the Automator plugin.
* This is useful for broader logging or actions that need to happen regardless of the specific option key.
*/
add_action( 'automator_option_added', function ( $key, $value ) {
// Log all option changes to a general audit log.
// Avoid excessive logging if this hook fires too frequently.
// error_log( sprintf( 'Automator: Generic option "%s" was added/updated.', $key ) );
// You could also check for specific keys here if needed, but using the keyed hook is more performant.
if ( 'some_other_setting' === $key ) {
// Do something specific for 'some_other_setting'.
// e.g., flush rewrite rules if it affects permalinks.
// flush_rewrite_rules();
}
}, 10, 2 ); // Priority 10, accepts 2 arguments ($key, $value)
?>
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:344
public function add_option( string $key, $value, bool $autoload = true, bool $run_action_hooks = true ) {
$key = $this->validate_key( $key );
if ( false === $key ) {
return false;
}
// Check if option already exists in cache or database.
$existing = $this->cache->get_from_memory( $key, Automator_Options_Cache::ABSENT );
if ( Automator_Options_Cache::ABSENT !== $existing && Automator_Options_Cache::MISS !== $existing ) {
return false; // exists in memory/autoloaded
}
// Check database.
if ( $this->query->option_exists( $key ) ) {
return false;
}
// Fire actions before adding or updating the option.
if ( $run_action_hooks ) {
do_action( 'automator_before_add_option', $key, $value, $autoload );
}
// Add to database.
$encoded_value = Automator_Option_Formatter::encode_value( $value );
$serialized_value = maybe_serialize( $encoded_value );
if ( false === $this->query->insert_option( $key, $serialized_value, $autoload ) ) {
return false;
}
// Delete any existing cache entries.
$this->cache->delete( $key );
// Update cache with new value.
$this->cache->set( $key, $value, $serialized_value, $autoload );
// Fire post-add/update actions.
if ( $run_action_hooks ) {
do_action( "automator_add_option_{$key}", $key, $value );
do_action( 'automator_option_added', $key, $value );
}
return true;
}