Using WordPress ‘add_site_option’ PHP action

The add_site_option WordPress action fires after a network option has been successfully added.

Usage

You can use the add_site_option function to run custom code after a network option has been added. Here’s a generic example of how to use this action:

add_action( 'add_site_option', function( $option, $value, $network_id ) {
  // your custom code here
  return $option;
}, 10, 3 );

In this example, we’ve used the add_action function to add a callback function that runs when the add_site_option action is triggered. The callback function takes three parameters – $option, $value, and $network_id – and returns the $option parameter.

Parameters

The add_site_option action takes the following parameters:

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

More information

For more information on using the add_site_option action, please see the WordPress Developer Resources page.

Note that this action is only triggered after a network option has been successfully added, and not during the process of adding a network option.

Examples

Add custom code to update a network option

In this example, we’ll add custom code to update a network option called ‘my_network_option’:

add_action( 'add_site_option', function( $option, $value, $network_id ) {
  if ( $option === 'my_network_option' ) {
    // your custom code here
  }
}, 10, 3 );

In this example, we’re checking if the $option parameter is equal to ‘my_network_option’, and if it is, we run our custom code.

Add custom code to log network option updates

In this example, we’ll add custom code to log network option updates to a file:

add_action( 'add_site_option', function( $option, $value, $network_id ) {
$log = date( 'Y-m-d H:i:s' ) . ' - ' . $option . ': ' . print_r( $value, true ) . "\n";
file_put_contents( '/path/to/log/file.txt', $log, FILE_APPEND );
}, 10, 3 );

In this example, we’re using the file_put_contents function to append a log message to a file every time a network option is added. The log message includes the current date and time, the name of the network option, and the value of the network option.

Add custom code to send an email notification on network option updates

In this example, we’ll add custom code to send an email notification to the site administrator every time a network option is added:

add_action( 'add_site_option', function( $option, $value, $network_id ) {
$subject = 'Network Option Added: ' . $option;
$message = 'A new network option has been added: ' . $option . "\n\n";
$message .= 'Value: ' . print_r( $value, true );
wp_mail( get_option( 'admin_email' ), $subject, $message );
}, 10, 3 );

In this example, we’re using the wp_mail function to send an email notification to the site administrator every time a network option is added. The email includes the name of the network option and the value of the network option.

Add custom code to update multiple network options

In this example, we’ll add custom code to update multiple network options at once:

add_action( 'add_site_option', function( $option, $value, $network_id ) {
$options_to_update = array(
'option1' => 'value1',
'option2' => 'value2',
'option3' => 'value3',
);
if ( array_key_exists( $option, $options_to_update ) ) {
update_site_option( $option, $options_to_update[ $option ] );
}
}, 10, 3 );

In this example, we’re using an array called $options_to_update to store the names and values of multiple network options that we want to update. We’re then checking if the $option parameter is one of the options we want to update, and if it is, we’re using the update_site_option function to update the network option.

Add custom code to prevent certain network options from being added

In this example, we’ll add custom code to prevent certain network options from being added:

add_action( 'add_site_option', function( $option, $value, $network_id ) {
$options_to_exclude = array( 'option1', 'option2', 'option3' );
if ( in_array( $option, $options_to_exclude ) ) {
return false;
}
}, 10, 3 );

In this example, we’re using an array called $options_to_exclude to store the names of network options that we want to exclude from being added. We’re then checking if the $option parameter is one of the options we want to exclude, and if it is, we’re returning false, which prevents the network option from being added.