Using WordPress ‘get_network_option()’ PHP function

The get_network_option() WordPress PHP function retrieves a network’s option value based on the option name.

Usage

get_network_option( $network_id, $option, $default_value = false );

Parameters

  • $network_id (int) – ID of the network. Can be null to default to the current network ID.
  • $option (string) – Name of the option to retrieve. Expected to not be SQL-escaped.
  • $default_value (mixed) – Optional. Value to return if the option doesn’t exist. Default: false

More information

See WordPress Developer Resources: get_network_option()

Examples

Get a network option by its name

// Get the 'site_name' option for the current network
$site_name = get_network_option( null, 'site_name' );
echo "Site Name: " . $site_name;

Get a network option with a specified network ID

// Get the 'admin_email' option for network ID 2
$admin_email = get_network_option( 2, 'admin_email' );
echo "Admin Email: " . $admin_email;

Get a network option with a default value

// Get the 'custom_option' and provide a default value if it doesn't exist
$custom_option = get_network_option( null, 'custom_option', 'default_value' );
echo "Custom Option: " . $custom_option;

Check if a network option exists

$option_name = 'custom_option';

if ( get_network_option( null, $option_name, false ) !== false ) {
    echo "Option '{$option_name}' exists.";
} else {
    echo "Option '{$option_name}' does not exist.";
}

Display a network option with a fallback message

// Get the 'custom_message' and display a fallback message if it doesn't exist
$custom_message = get_network_option( null, 'custom_message', 'No custom message found.' );
echo "Custom Message: " . $custom_message;