add_network_option

Add a new network option.

Description

add_network_option( (int) $network_id, (string) $option, (mixed) $value ); 

Existing options will not be updated.

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 add. Expected to not be SQL-escaped.
2. $value (mixed)
Option value, can be anything. Expected to not be SQL-escaped.

Usage

  1. if ( !function_exists( 'add_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 add. Expected to not be SQL-escaped. 
  8. $option = ''; 
  9.  
  10. // Option value, can be anything. Expected to not be SQL-escaped. 
  11. $value = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = add_network_option($network_id, $option, $value); 
  15.  

Defined (1)

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

/wp-includes/option.php  
  1. function add_network_option( $network_id, $option, $value ) { 
  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. /** 
  15. * Filters the value of a specific network option before it is added. 
  16. * The dynamic portion of the hook name, `$option`, refers to the option name. 
  17. * @since 2.9.0 As 'pre_add_site_option_' . $key 
  18. * @since 3.0.0 
  19. * @since 4.4.0 The `$option` parameter was added. 
  20. * @since 4.7.0 The `$network_id` parameter was added. 
  21. * @param mixed $value Value of network option. 
  22. * @param string $option Option name. 
  23. * @param int $network_id ID of the network. 
  24. */ 
  25. $value = apply_filters( "pre_add_site_option_{$option}", $value, $option, $network_id ); 
  26.  
  27. $notoptions_key = "$network_id:notoptions"; 
  28.  
  29. if ( ! is_multisite() ) { 
  30. $result = add_option( $option, $value, '', 'no' ); 
  31. } else { 
  32. $cache_key = "$network_id:$option"; 
  33.  
  34. // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query 
  35. $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); 
  36. if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) { 
  37. if ( false !== get_network_option( $network_id, $option, false ) ) { 
  38. return false; 
  39.  
  40. $value = sanitize_option( $option, $value ); 
  41.  
  42. $serialized_value = maybe_serialize( $value ); 
  43. $result = $wpdb->insert( $wpdb->sitemeta, array( 'site_id' => $network_id, 'meta_key' => $option, 'meta_value' => $serialized_value ) ); 
  44.  
  45. if ( ! $result ) { 
  46. return false; 
  47.  
  48. wp_cache_set( $cache_key, $value, 'site-options' ); 
  49.  
  50. // This option exists now 
  51. $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // yes, again... we need it to be fresh 
  52. if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) { 
  53. unset( $notoptions[ $option ] ); 
  54. wp_cache_set( $notoptions_key, $notoptions, 'site-options' ); 
  55.  
  56. if ( $result ) { 
  57.  
  58. /** 
  59. * Fires after a specific network option has been successfully added. 
  60. * The dynamic portion of the hook name, `$option`, refers to the option name. 
  61. * @since 2.9.0 As "add_site_option_{$key}" 
  62. * @since 3.0.0 
  63. * @since 4.7.0 The `$network_id` parameter was added. 
  64. * @param string $option Name of the network option. 
  65. * @param mixed $value Value of the network option. 
  66. * @param int $network_id ID of the network. 
  67. */ 
  68. do_action( "add_site_option_{$option}", $option, $value, $network_id ); 
  69.  
  70. /** 
  71. * Fires after a network option has been successfully added. 
  72. * @since 3.0.0 
  73. * @since 4.7.0 The `$network_id` parameter was added. 
  74. * @param string $option Name of the network option. 
  75. * @param mixed $value Value of the network option. 
  76. * @param int $network_id ID of the network. 
  77. */ 
  78. do_action( 'add_site_option', $option, $value, $network_id ); 
  79.  
  80. return true; 
  81.  
  82. return false;