add_option

Add a new option.

Description

add_option( (string) $option, (string) $value = '', (string) $deprecated = '', (string) $autoload = 'yes' ); 

You do not need to serialize values. If the value needs to be serialized, then it will be serialized before it is inserted into the database. Remember, resources can not be serialized or added as an option.

You can create options without values and then update the values later. Existing options will not be updated and checks are performed to ensure that you aren't adding a protected WordPress option. Care should be taken to not name options the same as the ones which are protected.

Parameters (4)

0. $option (string)
Name of option to add. Expected to not be SQL-escaped.
1. $value — Optional. (string) => ''
Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
2. $deprecated — Optional. (string) => ''
Description. Not used anymore.
3. $autoload — Optional. (string) => 'yes'
Whether to load the option when WordPress starts up. Default is enabled. Accepts no to disable for legacy reasons.

Usage

  1. if ( !function_exists( 'add_option' ) ) { 
  2. require_once ABSPATH . WPINC . '/option.php'; 
  3.  
  4. // Name of option to add. Expected to not be SQL-escaped. 
  5. $option = ''; 
  6.  
  7. // Optional. Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped. 
  8. $value = ''; 
  9.  
  10. // Optional. Description. Not used anymore. 
  11. $deprecated = ''; 
  12.  
  13. // Optional. Whether to load the option when WordPress starts up. 
  14. // Default is enabled. Accepts 'no' to disable for legacy reasons. 
  15. $autoload = 'yes'; 
  16.  
  17. // NOTICE! Understand what this does before running. 
  18. $result = add_option($option, $value, $deprecated, $autoload); 
  19.  

Defined (1)

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

/wp-includes/option.php  
  1. function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) { 
  2. global $wpdb; 
  3.  
  4. if ( !empty( $deprecated ) ) 
  5. _deprecated_argument( __FUNCTION__, '2.3.0' ); 
  6.  
  7. $option = trim($option); 
  8. if ( empty($option) ) 
  9. return false; 
  10.  
  11.  
  12. if ( is_object($value) ) 
  13. $value = clone $value; 
  14.  
  15. $value = sanitize_option( $option, $value ); 
  16.  
  17. // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query 
  18. $notoptions = wp_cache_get( 'notoptions', 'options' ); 
  19. if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) ) 
  20. /** This filter is documented in wp-includes/option.php */ 
  21. if ( apply_filters( 'default_option_' . $option, false, $option ) !== get_option( $option ) ) 
  22. return false; 
  23.  
  24. $serialized_value = maybe_serialize( $value ); 
  25. $autoload = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes'; 
  26.  
  27. /** 
  28. * Fires before an option is added. 
  29. * @since 2.9.0 
  30. * @param string $option Name of the option to add. 
  31. * @param mixed $value Value of the option. 
  32. */ 
  33. do_action( 'add_option', $option, $value ); 
  34.  
  35. $result = $wpdb->query( $wpdb->prepare( "INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`), `option_value` = VALUES(`option_value`), `autoload` = VALUES(`autoload`)", $option, $serialized_value, $autoload ) ); 
  36. if ( ! $result ) 
  37. return false; 
  38.  
  39. if ( ! wp_installing() ) { 
  40. if ( 'yes' == $autoload ) { 
  41. $alloptions = wp_load_alloptions(); 
  42. $alloptions[ $option ] = $serialized_value; 
  43. wp_cache_set( 'alloptions', $alloptions, 'options' ); 
  44. } else { 
  45. wp_cache_set( $option, $serialized_value, 'options' ); 
  46.  
  47. // This option exists now 
  48. $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh 
  49. if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) { 
  50. unset( $notoptions[$option] ); 
  51. wp_cache_set( 'notoptions', $notoptions, 'options' ); 
  52.  
  53. /** 
  54. * Fires after a specific option has been added. 
  55. * The dynamic portion of the hook name, `$option`, refers to the option name. 
  56. * @since 2.5.0 As "add_option_{$name}" 
  57. * @since 3.0.0 
  58. * @param string $option Name of the option to add. 
  59. * @param mixed $value Value of the option. 
  60. */ 
  61. do_action( "add_option_{$option}", $option, $value ); 
  62.  
  63. /** 
  64. * Fires after an option has been added. 
  65. * @since 2.9.0 
  66. * @param string $option Name of the added option. 
  67. * @param mixed $value Value of the option. 
  68. */ 
  69. do_action( 'added_option', $option, $value ); 
  70. return true;