Using WordPress ‘added_option’ PHP action

The added_option WordPress PHP action fires after an option has been added.

Usage

add_action('added_option', 'my_custom_function', 10, 2);

function my_custom_function($option, $value) {
    // your custom code here
}

Parameters

  • $option (string) – Name of the added option.
  • $value (mixed) – Value of the option.

More information

See WordPress Developer Resources: added_option

Examples

Log added options

Log the added options to a log file.

add_action('added_option', 'log_added_option', 10, 2);

function log_added_option($option, $value) {
    error_log("Option added: {$option} - Value: " . json_encode($value));
}

Send an email notification

Send an email notification when a specific option is added.

add_action('added_option', 'email_on_added_option', 10, 2);

function email_on_added_option($option, $value) {
    if ($option === 'my_special_option') {
        wp_mail('[email protected]', 'A special option was added', "Option: {$option} - Value: " . json_encode($value));
    }
}

Update another option

Update another option when an option is added.

add_action('added_option', 'update_another_option', 10, 2);

function update_another_option($option, $value) {
    if ($option === 'my_option') {
        update_option('another_option', 'Updated because of my_option');
    }
}

Add a prefix to an option

Add a prefix to an option when it’s added.

add_action('added_option', 'add_option_prefix', 10, 2);

function add_option_prefix($option, $value) {
    if (strpos($option, 'myprefix_') !== 0) {
        update_option('myprefix_' . $option, $value);
        delete_option($option);
    }
}

Set an option to expire

Automatically set an option to expire after a certain time.

add_action('added_option', 'set_option_expiration', 10, 2);

function set_option_expiration($option, $value) {
    if ($option === 'temp_option') {
        wp_schedule_single_event(time() + 3600, 'expire_temp_option', array($option));
    }
}

add_action('expire_temp_option', 'delete_temp_option', 10, 1);

function delete_temp_option($option) {
    delete_option($option);
}