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