get_network_option

Retrieve a network's option value based on the option name.

Description

get_network_option( (int) $network_id, (string) $option, (bool) $default = false ); 

Parameters (3)

0. $network_id (int)
ID of the network. Can be null to default to the current network ID.
1. $option (string)
Name of option to retrieve. Expected to not be SQL-escaped.
2. $default — Optional. (bool) => false
Value to return if the option doesn't exist. Default false.

Usage

  1. if ( !function_exists( 'get_network_option' ) ) { 
  2. require_once ABSPATH . WPINC . '/option.php'; 
  3.  
  4. // ID of the network. Can be null to default to the current network ID. 
  5. $network_id = -1; 
  6.  
  7. // Name of option to retrieve. Expected to not be SQL-escaped. 
  8. $option = ''; 
  9.  
  10. // Optional. Value to return if the option doesn't exist. Default false. 
  11. $default = false; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = get_network_option($network_id, $option, $default); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/option.php  
  1. function get_network_option( $network_id, $option, $default = false ) { 
  2. global $wpdb; 
  3.  
  4. if ( $network_id && ! is_numeric( $network_id ) ) { 
  5. return false; 
  6.  
  7. $network_id = (int) $network_id; 
  8.  
  9. // Fallback to the current network if a network ID is not specified. 
  10. if ( ! $network_id ) { 
  11. $network_id = get_current_network_id(); 
  12.  
  13. /** 
  14. * Filters an existing network option before it is retrieved. 
  15. * The dynamic portion of the hook name, `$option`, refers to the option name. 
  16. * Passing a truthy value to the filter will effectively short-circuit retrieval,  
  17. * returning the passed value instead. 
  18. * @since 2.9.0 As 'pre_site_option_' . $key 
  19. * @since 3.0.0 
  20. * @since 4.4.0 The `$option` parameter was added. 
  21. * @since 4.7.0 The `$network_id` parameter was added. 
  22. * @param mixed $pre_option The default value to return if the option does not exist. 
  23. * @param string $option Option name. 
  24. * @param int $network_id ID of the network. 
  25. */ 
  26. $pre = apply_filters( "pre_site_option_{$option}", false, $option, $network_id ); 
  27.  
  28. if ( false !== $pre ) { 
  29. return $pre; 
  30.  
  31. // prevent non-existent options from triggering multiple queries 
  32. $notoptions_key = "$network_id:notoptions"; 
  33. $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); 
  34.  
  35. if ( isset( $notoptions[ $option ] ) ) { 
  36.  
  37. /** 
  38. * Filters a specific default network option. 
  39. * The dynamic portion of the hook name, `$option`, refers to the option name. 
  40. * @since 3.4.0 
  41. * @since 4.4.0 The `$option` parameter was added. 
  42. * @since 4.7.0 The `$network_id` parameter was added. 
  43. * @param mixed $default The value to return if the site option does not exist 
  44. * in the database. 
  45. * @param string $option Option name. 
  46. * @param int $network_id ID of the network. 
  47. */ 
  48. return apply_filters( "default_site_option_{$option}", $default, $option, $network_id ); 
  49.  
  50. if ( ! is_multisite() ) { 
  51. /** This filter is documented in wp-includes/option.php */ 
  52. $default = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id ); 
  53. $value = get_option( $option, $default ); 
  54. } else { 
  55. $cache_key = "$network_id:$option"; 
  56. $value = wp_cache_get( $cache_key, 'site-options' ); 
  57.  
  58. if ( ! isset( $value ) || false === $value ) { 
  59. $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) ); 
  60.  
  61. // Has to be get_row instead of get_var because of funkiness with 0, false, null values 
  62. if ( is_object( $row ) ) { 
  63. $value = $row->meta_value; 
  64. $value = maybe_unserialize( $value ); 
  65. wp_cache_set( $cache_key, $value, 'site-options' ); 
  66. } else { 
  67. if ( ! is_array( $notoptions ) ) { 
  68. $notoptions = array(); 
  69. $notoptions[ $option ] = true; 
  70. wp_cache_set( $notoptions_key, $notoptions, 'site-options' ); 
  71.  
  72. /** This filter is documented in wp-includes/option.php */ 
  73. $value = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id ); 
  74.  
  75. /** 
  76. * Filters the value of an existing network option. 
  77. * The dynamic portion of the hook name, `$option`, refers to the option name. 
  78. * @since 2.9.0 As 'site_option_' . $key 
  79. * @since 3.0.0 
  80. * @since 4.4.0 The `$option` parameter was added. 
  81. * @since 4.7.0 The `$network_id` parameter was added. 
  82. * @param mixed $value Value of network option. 
  83. * @param string $option Option name. 
  84. * @param int $network_id ID of the network. 
  85. */ 
  86. return apply_filters( "site_option_{$option}", $value, $option, $network_id );