Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use added_option action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
September 6, 2022
5 minutes read

added_option action

Fires after an option has been added.

To use the added_option action, first you have to register it using add_action. You can write this code into functions.php of your activated theme or in a custom WordPress Plugin.

We at WePlugins always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.

In the below live example, we have defined a function execute_on_added_option_event which takes 2 parameters and we registered using add_action. The first parameter added_option is the name of the hook, the second parameter execute_on_added_option_event is the name of the function which needs to be called, the third parameter is the priority of calling the hook if the same hook is used multiple times, and the last parameter is the number of arguments (if any) to be passed in the registered function.

Sometimes, you have to remove a registered hook so you can use remove_action to remove the added_option action.

Parameters

    Below are the 2 parameters required to use this hook.

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

Live Example

do_action('added_option', string $option, mixed $value)

Below is an example of how you can use this hook.

    function weplugins_execute_on_added_option_event($option, $value){
       // You can write code here to be executed when this action occurs in WordPress.
       // Use the parameters received in the function arguments & implement the required additional custom functionality according to your website requirements.
    }
    // add the action
    add_action('added_option', 'weplugins_execute_on_added_option_event', 10, 2);
    

To remove a hook callback, use the example below.

remove_action('added_option', 'weplugins_execute_on_added_option_event', 10, 2);

Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.

Access Premium WordPress Plugins

Example 1: Logging Added Options

In this example, we log the added options to a custom log file for debugging purposes.

    function weplugins_log_added_option($option, $value){
        $log = fopen(WP_CONTENT_DIR . '/weplugins_options.log', 'a');
        fwrite($log, "Added option: $option with value: " . print_r($value, true) . "n");
        fclose($log);
    }
    add_action('added_option', 'weplugins_log_added_option', 10, 2);
    

Example 2: Notify Admin on Option Add

This example sends an email to the admin whenever a new option is added.

    function weplugins_notify_admin_on_added_option($option, $value){
        $admin_email = get_option('admin_email');
        $subject = "New Option Added: $option";
        $message = "A new option has been added with the value: " . print_r($value, true);
        wp_mail($admin_email, $subject, $message);
    }
    add_action('added_option', 'weplugins_notify_admin_on_added_option', 10, 2);
    

Example 3: Update Related Option

Here, we update another option whenever a specific option is added.

    function weplugins_update_related_option($option, $value){
        if($option === 'specific_option_name'){
            update_option('related_option_name', 'new_value');
        }
    }
    add_action('added_option', 'weplugins_update_related_option', 10, 2);
    

Contact Us

If you need any customization or have trouble using this hook, please Contact Us.

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.