Using WordPress ‘add_site_option_{$option}’ PHP action

The add_site_option_{$option} WordPress action fires after a specific network option has been successfully added.

Usage

Here’s an example of how to use the add_site_option_{$option} action in your WordPress PHP code:

add_action( 'add_site_option_$option', 'my_network_option_added', 10, 3 );

function my_network_option_added( $option, $value, $network_id ) {
    // your custom code here
    return $value;
}

Parameters

  • $option (string): The name of the network option.
  • $value (mixed): The value of the network option.
  • $network_id (int): The ID of the network.

More information

See WordPress Developer Resources: add_site_option_{$option} for more information on using this action.

This action is useful if you want to perform some custom code after a network option has been added. You can access the option name, value and network ID using the function parameters.

Examples

Sending an email notification when a network option is added

This example shows how you can use the add_site_option_{$option} action to send an email notification when a network option is added.

add_action( 'add_site_option_$option', 'send_notification_on_network_option_added', 10, 3 );

function send_notification_on_network_option_added( $option, $value, $network_id ) {
    $to = '[email protected]';
    $subject = 'New network option added';
    $message = "The network option $option has been added with the value $value on network $network_id.";
    wp_mail( $to, $subject, $message );
}

Updating a network option after it has been added

This example shows how you can use the add_site_option_{$option} action to update a network option after it has been added.

add_action( 'add_site_option_$option', 'update_network_option', 10, 3 );

function update_network_option( $option, $value, $network_id ) {
    // your custom code here
    $new_value = 'new_value';
    update_site_option( $option, $new_value );
}

Logging network option changes to a file

This example shows how you can use the add_site_option_{$option} action to log network option changes to a file.

add_action( 'add_site_option_$option', 'log_network_option_changes', 10, 3 );

function log_network_option_changes( $option, $value, $network_id ) {
    $log_file = 'network_options.log';
    $log_message = "Option $option added with value $value on network $network_id.";
    error_log( $log_message, 3, $log_file );
}

Displaying a success message after adding a network option

This example shows how you can use the add_site_option_{$option} action to display a success message after adding a network option.

add_action( 'add_site_option_$option', 'display_success_message', 10, 3 );

function display_success_message( $option, $value, $network_id ) {
    $message = "The option $option has been added with the value $value on network $network_id.";
    echo '<div class="notice notice-success"><p>' . $message . '</p></div>';
}