The add_option_{$option} WordPress PHP action fires after a specific option has been added. The dynamic portion of the hook name, $option
, refers to the option name.
Usage
add_action('add_option_my_custom_option', 'my_custom_function', 10, 2); function my_custom_function($option, $value) { // your custom code here }
Parameters
$option
(string) – Name of the option to add.$value
(mixed) – Value of the option.
More information
See WordPress Developer Resources: add_option_{$option}
Examples
Logging added options
Log the added options and their values to a log file.
add_action('add_option_my_option1', 'log_added_option', 10, 2); function log_added_option($option, $value) { error_log("Added option: {$option} with value: " . print_r($value, true)); }
Send an email when a specific option is added
Send an email to the admin when the “my_email_option” is added.
add_action('add_option_my_email_option', 'send_email_on_option_add', 10, 2); function send_email_on_option_add($option, $value) { wp_mail(get_option('admin_email'), 'New Option Added', "Option '{$option}' added with value: {$value}"); }
Update another option when an option is added
Update the “my_updated_option” value when “my_initial_option” is added.
add_action('add_option_my_initial_option', 'update_another_option', 10, 2); function update_another_option($option, $value) { update_option('my_updated_option', $value); }
Add a prefix to a newly added option value
Add a prefix to the value of “my_prefixed_option” when it is added.
add_action('add_option_my_prefixed_option', 'add_prefix_to_option_value', 10, 2); function add_prefix_to_option_value($option, $value) { update_option($option, 'prefix_' . $value); }
Set an expiration time for a transient when a specific option is added
Set an expiration time for the “my_expiring_transient” when “my_expiration_option” is added.
add_action('add_option_my_expiration_option', 'set_expiration_transient', 10, 2); function set_expiration_transient($option, $value) { set_transient('my_expiring_transient', $value, 60 * 60 * 24); }