The add_site_option() WordPress PHP function is used to add a new option for the current network. If the option already exists, it will not be updated. This function can take any value for the option, and it expects the name of the option to not be SQL-escaped.
Usage
A typical usage of the function looks like this:
add_site_option('your_option_name', 'your_option_value');
Parameters
- $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 to add. It can be anything and it is also expected to not be SQL-escaped.
More Information
See WordPress Developer Resources: add_site_option()
This function was implemented before version 3.3. It works in conjunction with add_network_option()
and get_site_option()
functions.
Examples
Add new site option
This code adds a new site option ‘my_option’ with the value ‘my_value’.
add_site_option('my_option', 'my_value');
Check if option already exists
This code checks if the option ‘i_exist_already’ exists. If it does, it will print ‘Already exists’, otherwise it adds the option with ‘new_value’.
if (!add_site_option('i_exist_already', 'new_value')) { echo __('Already exists', 'textdomain'); }
Setting default options
This example sets the ‘wporg_lead_options’ to default values only if the option doesn’t exist.
if (!get_site_option('wporg_lead_options')) { add_site_option('wporg_lead_options', $wporg_lead_options_defaults); }
Add site option with array value
This code adds a site option ‘my_option’ with an array value.
add_site_option('my_option', array('key1' => 'value1', 'key2' => 'value2'));
Attempt to overwrite existing option
This example attempts to overwrite the existing ‘my_option’ with a new value ‘new_value’, but it won’t succeed because add_site_option
doesn’t update existing options.
add_site_option('my_option', 'new_value');