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

How to use add_option_option action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
January 15, 2023
5 minutes read

add_option_option action

The dynamic portion of the hook name, $option, refers to the option name.

To use add_option_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 weplugins_execute_on_add_option_option_event which takes 2 parameters and we registered it using add_action. The first parameter add_option_option is the name of the hook, the second parameter weplugins_execute_on_add_option_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 add_option_option action.

Parameters

    Below are the 2 parameters required to use this hook.

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

Live Example

do_action( "add_option_{$option}", string $option, mixed $value )

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

Example 1: Basic Usage

This example demonstrates the basic usage of the add_option_option hook.

    function weplugins_execute_on_add_option_option_event($option, $value){
       // Code to be executed when this action occurs in WordPress. 
       // Use the parameters received in the function arguments and implement the required custom functionality.
    }
    // add the action
    add_action( "add_option_option", "weplugins_execute_on_add_option_option_event" , 10, 2);
    

Example 2: Logging Added Options

This example logs the name and value of each added option for debugging purposes.

    function weplugins_log_added_option($option, $value){
       error_log("Option Added: " . $option . " with value: " . print_r($value, true));
    }
    // add the action
    add_action( "add_option_option", "weplugins_log_added_option" , 10, 2);
    

Example 3: Conditional Execution

This example performs a different action based on the option name.

    function weplugins_conditional_execution($option, $value){
       if ($option == 'special_option') {
           // Do something special
           update_option('another_option', 'new value');
       }
    }
    // add the action
    add_action( "add_option_option", "weplugins_conditional_execution" , 10, 2);
    

To remove a hook callback, use the example below.

remove_action( "add_option_option", "weplugins_execute_on_add_option_option_event", 10, 2 );

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

Contact Us

If you’re having any trouble using this hook, please contact our WordPress Development Team and we’d be happy to assist you.

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.