Using WordPress ‘add_network_option()’ PHP function

The add_network_option() WordPress PHP function adds a new network option. This function will not update existing options.

Usage

Let’s say you want to add a network option named “default_theme” with the value “twentytwentytwo” for network ID 1. Here’s how you’d do it:

add_network_option(1, 'default_theme', 'twentytwentytwo');

After running this line of code, the option ‘default_theme’ will be set to ‘twentytwentytwo’ for network ID 1.

Parameters

  • $network_id (int): This is the ID of the network. This can be null, in which case it defaults to the current network ID.
  • $option (string): This is the name of the option to add. It is expected to not be SQL-escaped.
  • $value (mixed): This is the value of the option. It can be anything and is also expected to not be SQL-escaped.

More information

See WordPress Developer Resources: add_network_option()
This function was implemented in WordPress 3.4.0. Source code for this function can be found in wp-includes/option.php.

Examples

Adding a New Option

This code adds a new option named “max_users” with the value 500 for network ID 1.

add_network_option(1, 'max_users', 500);

Setting a Default Network

This code adds an option named “default_network” with the value 1, defaulting to the current network ID if it is null.

add_network_option(null, 'default_network', 1);

Adding a Boolean Option

This code adds a new boolean option named “user_registration” with the value false for network ID 2.

add_network_option(2, 'user_registration', false);

Adding an Array Option

This code adds a new option named “allowed_themes” with an array of theme names for network ID 3.

add_network_option(3, 'allowed_themes', array('twentytwenty', 'twentynineteen'));

Adding an Option with a Complex Value

This code adds a new option named “front_page_settings” with an associative array as its value for network ID 1.

add_network_option(1, 'front_page_settings', array('show_on_front' => 'page', 'page_on_front' => 7));