add_site_option() wordpress function and usage This text is not used because the data attribute has a value

add_site_option() wordpress function and usage

Home / WordPress / WordPress Wiki / add_site_option() wordpress function and usage

add_site_option( string $option, mixed $value )
Adds a new option for the current network.

Description

Existing options will not be updated. Note that prior to 3.3 this wasn’t the case.

See also

  • add_network_option()

Parameters

$option

(string) (Required) Name of the option to add. Expected to not be SQL-escaped.

$value

(mixed) (Required) Option value, can be anything. Expected to not be SQL-escaped.

Return

(bool) True if the option was added, false otherwise.

More Information

This function essentially the same as add_option() but works network wide when using WP Multisite.

The only major difference is that on multisite site-wide options will not autoload and on a single site the option will autoload. Unlike when using add_option() on a single site, the feature cannot be overridden.

Sample Usage

Install the option defaults

if ( 1 == $wpmu ) {
     if ( ! get_site_option( 'wporg_lead_options' ) ) {
         add_site_option( 'wporg_lead_options', $wporg_lead_options_defaults, '', 'yes' );
     }
} else {
     if ( ! get_option( 'wporg_lead_options' ) ) {
         add_option( 'wporg_lead_options', $wporg_lead_options_defaults, '', 'yes' );
     }
}

Examples

Default usage:

add_site_option( 'my_option', 'my_value' );

Behavior if the option already exists:

echo get_site_option( 'i_exist_already' );
 
// Output: 'some_value'
 
 
if ( add_site_option( 'i_exist_already', 'new_value' ) ) {
   echo get_site_option( 'i_exist_already' );
} else {
   echo __( 'Already exists', 'textdomain' );
}

 

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments