Using add_network_option to add network-wide options in WordPress

The add_network_option function is a part of WordPress’s multi-site network API. This function allows you to add a new network-wide option. If the option already exists, the function will not replace the existing value unless you explicitly instruct it to do so. This function is primarily used in a multi-site network environment.

By using the add_network_option function, it becomes possible to store data that is not specific to a single site, but rather applies to the entire network. This function allows for the creation of network-wide settings, and can be instrumental in managing and customizing the behavior of a multi-site network.

It’s important to note that the add_network_option function is only available when the WordPress setup is configured to operate as a multi-site network. If the function is used on a regular WordPress installation, it will not perform any action.

Parameters Accepted by the add_network_option Function

The add_network_option function in WordPress accepts three parameters:

  • $network_id (integer): This is a required parameter that represents the ID of the network. If it is set to null, it defaults to the current network ID.
  • $option (string): This required parameter signifies the name of the option to be added. It is expected that this parameter will not be SQL-escaped.
  • $value (mixed): This required parameter represents the value of the option to be added. This can be any value and it is also expected not to be SQL-escaped.

Return Value of the add_network_option Function

The add_network_option function returns a boolean value. This function will return true if the option was successfully added. If the option was not added, the function will return false.

If the function does not accept any parameters, it will be stated clearly and concisely.

Examples

How to Add a Network Option in WordPress

The add_network_option function is commonly used to add a new option to the options database table in a multisite network. This is especially useful when you want to store site-wide settings. Below is a basic usage of the function:

$network_id = null; // Current network
$option = 'my_network_option';
$value = 'This is my network option value';

$added = add_network_option($network_id, $option, $value);

if ($added) {
 echo 'Option added successfully.';
} else {
 echo 'Failed to add option.';
}

In this code snippet, we are adding a new option named my_network_option with the value ‘This is my network option value’ to the current network. If the option is successfully added, it will print ‘Option added successfully.’ Otherwise, it will print ‘Failed to add option.’

How to Add a Network Option to a Specific Network in WordPress

Here’s an example of how to add a network option to a specific network:

$network_id = 2; // Specific network
$option = 'my_specific_network_option';
$value = 'This is my specific network option value';

$added = add_network_option($network_id, $option, $value);

if ($added) {
 echo 'Option added successfully to network 2.';
} else {
 echo 'Failed to add option to network 2.';
}

In this code snippet, we are adding a new option named my_specific_network_option with the value ‘This is my specific network option value’ to the network with the ID of 2. If the option is successfully added, it will print ‘Option added successfully to network 2.’ Otherwise, it will print ‘Failed to add option to network 2.’

How to Add a Complex Network Option in WordPress

The add_network_option function can also handle complex data types, like arrays or objects. Here’s an example:

$network_id = null; // Current network
$option = 'my_complex_network_option';
$value = array('key1' => 'value1', 'key2' => 'value2');

$added = add_network_option($network_id, $option, $value);

if ($added) {
 echo 'Complex option added successfully.';
} else {
 echo 'Failed to add complex option.';
}

In this code snippet, we are adding a new option named my_complex_network_option with an array as a value to the current network. If the option is successfully added, it will print ‘Complex option added successfully.’ Otherwise, it will print ‘Failed to add complex option.’

Conclusion

The add_network_option function in WordPress is integral to adding a new option for the entire network. It allows developers to store network-wide settings that apply to all sites in a network, rather than individual settings for each site. This functionality is particularly useful in a multisite environment, where you may need to set or adjust options that are consistent across all sites. Utilizing the add_network_option function can help to streamline and simplify the management of network-wide settings.

Related WordPress Functions