Action
uncanny-automator
automator_before_add_option
Fires before an option is added to the database, allowing modification of its key, value, or autoload setting.
add_action( 'automator_before_add_option', $callback, 10, 3 );
Description
Fires before an option is added to the Automator plugin's database. Developers can use this hook to modify the option key, value, or autoload status, or to perform other actions before the option is saved. The original values are available for manipulation.
Usage
add_action( 'automator_before_add_option', 'your_function_name', 10, 3 );
Parameters
-
$key(mixed) - This parameter represents the unique identifier for the option being added.
-
$value(mixed) - This parameter represents the unique identifier or name of the option to be added.
-
$autoload(mixed) - This parameter holds the value that will be stored for the given option key.
Examples
/**
* Example of using the automator_before_add_option hook.
*
* This function demonstrates how to intercept an option being added by the Automator plugin
* before it's saved to the database. In this example, we'll log the key and value of
* the option being added, and if the option key is 'my_sensitive_data', we'll prevent
* its addition by returning false.
*/
add_action( 'automator_before_add_option', function ( $key, $value, $autoload ) {
// Log the option key and value being added.
error_log( sprintf( 'Automator: Attempting to add option "%s" with value: %s', $key, print_r( $value, true ) ) );
// Example: Prevent adding a specific sensitive option.
if ( 'my_sensitive_data' === $key ) {
error_log( 'Automator: Preventing addition of sensitive option "my_sensitive_data".' );
// Returning false from a filter hook stops further processing.
// While this is an action hook, returning false here won't directly stop the
// option from being added in the current implementation, but it's a common
// pattern to show how to signal a condition. If this were a filter, returning
// false would indeed stop the action.
// For a true stop on an action hook, you'd need to unhook or use a global flag.
// This example illustrates the hook's parameters.
}
// You could also modify the $value or $autoload here if needed,
// but since this is an action hook, modifications won't be directly
// reflected in the $key, $value, or $autoload passed to the original
// `do_action` call within the plugin.
// If you needed to modify and have those changes used, it would likely
// need to be a filter hook instead.
}, 10, 3 ); // Priority 10, accepts 3 arguments.
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:325
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;
}