wppaste
WordPress

add_option( string $option, mixed $value = '', string $deprecated = '', bool|null $autoload = null ): bool

Since
1.0.0, 6.6.0, 6.7.0
Source
wp-includes/option.php:1061
Adds a new option.

Description

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 cannot 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.

Compatibility

WordPress
since 6.7.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$optionstring
Name of the option to add. Expected to not be SQL-escaped.
$valuemixedoptional
Option value. Must be serializable if non-scalar.
Expected to not be SQL-escaped.Default: ''
$deprecatedstringoptional
Description. Not used anymore.Default: ''
$autoloadbool|nulloptional
Whether to load the option when WordPress starts up.
Accepts a boolean, or null to leave the decision up to default heuristics in WordPress. For backward compatibility 'yes' and 'no' are also accepted, though using these values is deprecated.
Autoloading too many options can lead to performance problems, especially if the options are not frequently used. For options which are accessed across several places in the frontend, it is recommended to autoload them, by using true.
For options which are accessed only on few specific URLs, it is recommended to not autoload them, by using false.
Default is null, which means WordPress will determine the autoload value.Default: null

Return value

bool
True if the option was added, false otherwise.

Performance profile

How much work a call to add_option() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via ->query().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
12–134

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 165.

Plugin surface
4 hooks

Third-party callbacks on 'default_option_{$option}', 'add_option', 'add_option_{$option}' run inside this call, and their cost is not bounded by anything here.

Called by
9

9 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionoption read or writeadd_option()this function does it
  • cacheobject cachewp_cache_get()called directly
  • hookthird-party callbacksapply_filters()called directly
  • serializeserialisationmaybe_serialize()called directly
  • sqldatabase query->query()called directly

Further down the call graph this can also reach query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 64 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost add_option() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
empty($deprecated) && empty($option)12–14none
!empty($deprecated) && empty($option)16–18_deprecated_argument()
empty($deprecated) && !empty($option) && isset([…][$option]) && !wp_installing()40–42wp_installing(), __(), sprintf(), _deprecated_argument(), add_option()
empty($deprecated) && !empty($option) && !isset([…][$option])44–50wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option()
!empty($deprecated) && !empty($option) && isset([…][$option]) && !wp_installing()44–46_deprecated_argument(), wp_installing(), __(), sprintf(), _deprecated_argument(), add_option()
empty($deprecated) && !empty($option) && isset([…][$option]) && wp_installing()47–53wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option()
!empty($deprecated) && !empty($option) && !isset([…][$option])48–54_deprecated_argument(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option()
!empty($deprecated) && !empty($option) && isset([…][$option]) && wp_installing()51–57_deprecated_argument(), wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option()
empty($deprecated) && !empty($option) && !isset([…][$option]) && is_array($notoptions) && isset($notoptions[$option])65–69wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query()
empty($deprecated) && !empty($option) && isset([…][$option]) && wp_installing() && is_array($notoptions) && isset($notoptions[$option])68–72wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query()
!empty($deprecated) && !empty($option) && !isset([…][$option]) && is_array($notoptions) && isset($notoptions[$option])69–73_deprecated_argument(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query()
!empty($deprecated) && !empty($option) && isset([…][$option]) && wp_installing() && is_array($notoptions) && isset($notoptions[$option])72–76_deprecated_argument(), wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query()
52 further outcomes, up to 134 instructions
empty($deprecated) && !empty($option) && !isset([…][$option])75–81wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query()
empty($deprecated) && !empty($option) && isset([…][$option]) && wp_installing()78–84wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query()
!empty($deprecated) && !empty($option) && !isset([…][$option])79–85_deprecated_argument(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query()
!empty($deprecated) && !empty($option) && isset([…][$option]) && wp_installing()82–88_deprecated_argument(), wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query()
empty($deprecated) && !empty($option) && !isset([…][$option]) && is_array($notoptions) && isset($notoptions[$option]) && wp_installing()86–92wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_cache_get(), do_action(), do_action()
empty($deprecated) && !empty($option) && isset([…][$option]) && wp_installing() && is_array($notoptions) && isset($notoptions[$option])89–95wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_cache_get(), do_action(), do_action()
!empty($deprecated) && !empty($option) && !isset([…][$option]) && is_array($notoptions) && isset($notoptions[$option]) && wp_installing()90–96_deprecated_argument(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_cache_get(), do_action(), do_action()
!empty($deprecated) && !empty($option) && isset([…][$option]) && wp_installing() && is_array($notoptions) && isset($notoptions[$option])93–99_deprecated_argument(), wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_cache_get(), do_action(), do_action()
empty($deprecated) && !empty($option) && !isset([…][$option]) && is_array($notoptions) && isset($notoptions[$option]) && wp_installing()94–98wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
empty($deprecated) && !empty($option) && !isset([…][$option]) && wp_installing()96–104wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_cache_get(), do_action(), do_action()
empty($deprecated) && !empty($option) && !isset([…][$option]) && is_array($notoptions) && isset($notoptions[$option]) && !wp_installing() && !$autoload96–102wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_cache_set(), wp_cache_get(), do_action(), do_action()
empty($deprecated) && !empty($option) && isset([…][$option]) && wp_installing() && is_array($notoptions) && isset($notoptions[$option])97–101wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
!empty($deprecated) && !empty($option) && !isset([…][$option]) && is_array($notoptions) && isset($notoptions[$option]) && wp_installing()98–102_deprecated_argument(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
empty($deprecated) && !empty($option) && isset([…][$option]) && wp_installing()99–107wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_cache_get(), do_action(), do_action()
empty($deprecated) && !empty($option) && isset([…][$option]) && is_array($notoptions) && isset($notoptions[$option]) && !$autoload99–105wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_cache_set(), wp_cache_get(), do_action(), do_action()
!empty($deprecated) && !empty($option) && !isset([…][$option]) && wp_installing()100–108_deprecated_argument(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_cache_get(), do_action(), do_action()
!empty($deprecated) && !empty($option) && !isset([…][$option]) && is_array($notoptions) && isset($notoptions[$option]) && !wp_installing() && !$autoload100–106_deprecated_argument(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_cache_set(), wp_cache_get(), do_action(), do_action()
!empty($deprecated) && !empty($option) && isset([…][$option]) && wp_installing() && is_array($notoptions) && isset($notoptions[$option])101–105_deprecated_argument(), wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
empty($deprecated) && !empty($option) && !isset([…][$option]) && is_array($notoptions) && isset($notoptions[$option]) && !wp_installing() && $autoload103–109wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_load_alloptions(), wp_cache_set(), wp_cache_get(), do_action(), do_action()
!empty($deprecated) && !empty($option) && isset([…][$option]) && wp_installing()103–111_deprecated_argument(), wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_cache_get(), do_action(), do_action()
!empty($deprecated) && !empty($option) && isset([…][$option]) && is_array($notoptions) && isset($notoptions[$option]) && !$autoload103–109_deprecated_argument(), wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_cache_set(), wp_cache_get(), do_action(), do_action()
empty($deprecated) && !empty($option) && !isset([…][$option]) && wp_installing() && is_array($notoptions) && isset($notoptions[$option])104–110wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
empty($deprecated) && !empty($option) && !isset([…][$option]) && is_array($notoptions) && isset($notoptions[$option]) && !wp_installing() && !$autoload104–108wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_cache_set(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
empty($deprecated) && !empty($option) && !isset([…][$option]) && !wp_installing() && !$autoload106–114wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_cache_set(), wp_cache_get(), do_action(), do_action()
empty($deprecated) && !empty($option) && isset([…][$option]) && is_array($notoptions) && isset($notoptions[$option]) && $autoload106–112wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_load_alloptions(), wp_cache_set(), wp_cache_get(), do_action(), do_action()
empty($deprecated) && !empty($option) && isset([…][$option]) && wp_installing() && is_array($notoptions) && isset($notoptions[$option])107–113wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
empty($deprecated) && !empty($option) && isset([…][$option]) && is_array($notoptions) && isset($notoptions[$option]) && !$autoload107–111wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_cache_set(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
!empty($deprecated) && !empty($option) && !isset([…][$option]) && is_array($notoptions) && isset($notoptions[$option]) && !wp_installing() && $autoload107–113_deprecated_argument(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_load_alloptions(), wp_cache_set(), wp_cache_get(), do_action(), do_action()
!empty($deprecated) && !empty($option) && !isset([…][$option]) && wp_installing() && is_array($notoptions) && isset($notoptions[$option])108–114_deprecated_argument(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
!empty($deprecated) && !empty($option) && !isset([…][$option]) && is_array($notoptions) && isset($notoptions[$option]) && !wp_installing() && !$autoload108–112_deprecated_argument(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_cache_set(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
empty($deprecated) && !empty($option) && isset([…][$option]) && !$autoload109–117wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_cache_set(), wp_cache_get(), do_action(), do_action()
!empty($deprecated) && !empty($option) && !isset([…][$option]) && !wp_installing() && !$autoload110–118_deprecated_argument(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_cache_set(), wp_cache_get(), do_action(), do_action()
!empty($deprecated) && !empty($option) && isset([…][$option]) && is_array($notoptions) && isset($notoptions[$option]) && $autoload110–116_deprecated_argument(), wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_load_alloptions(), wp_cache_set(), wp_cache_get(), do_action(), do_action()
empty($deprecated) && !empty($option) && !isset([…][$option]) && is_array($notoptions) && isset($notoptions[$option]) && !wp_installing() && $autoload111–115wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_load_alloptions(), wp_cache_set(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
!empty($deprecated) && !empty($option) && isset([…][$option]) && wp_installing() && is_array($notoptions) && isset($notoptions[$option])111–117_deprecated_argument(), wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
!empty($deprecated) && !empty($option) && isset([…][$option]) && is_array($notoptions) && isset($notoptions[$option]) && !$autoload111–115_deprecated_argument(), wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_cache_set(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
empty($deprecated) && !empty($option) && !isset([…][$option]) && !wp_installing() && $autoload113–121wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_load_alloptions(), wp_cache_set(), wp_cache_get(), do_action(), do_action()
!empty($deprecated) && !empty($option) && isset([…][$option]) && !$autoload113–121_deprecated_argument(), wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_cache_set(), wp_cache_get(), do_action(), do_action()
empty($deprecated) && !empty($option) && !isset([…][$option]) && !wp_installing() && !$autoload && is_array($notoptions) && isset($notoptions[$option])114–120wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_cache_set(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
empty($deprecated) && !empty($option) && isset([…][$option]) && is_array($notoptions) && isset($notoptions[$option]) && $autoload114–118wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_load_alloptions(), wp_cache_set(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
!empty($deprecated) && !empty($option) && !isset([…][$option]) && is_array($notoptions) && isset($notoptions[$option]) && !wp_installing() && $autoload115–119_deprecated_argument(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_load_alloptions(), wp_cache_set(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
empty($deprecated) && !empty($option) && isset([…][$option]) && $autoload116–124wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_load_alloptions(), wp_cache_set(), wp_cache_get(), do_action(), do_action()
empty($deprecated) && !empty($option) && isset([…][$option]) && !$autoload && is_array($notoptions) && isset($notoptions[$option])117–123wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_cache_set(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
!empty($deprecated) && !empty($option) && !isset([…][$option]) && !wp_installing() && $autoload117–125_deprecated_argument(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_load_alloptions(), wp_cache_set(), wp_cache_get(), do_action(), do_action()
!empty($deprecated) && !empty($option) && !isset([…][$option]) && !wp_installing() && !$autoload && is_array($notoptions) && isset($notoptions[$option])118–124_deprecated_argument(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_cache_set(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
!empty($deprecated) && !empty($option) && isset([…][$option]) && is_array($notoptions) && isset($notoptions[$option]) && $autoload118–122_deprecated_argument(), wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_load_alloptions(), wp_cache_set(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
!empty($deprecated) && !empty($option) && isset([…][$option]) && $autoload120–128_deprecated_argument(), wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_load_alloptions(), wp_cache_set(), wp_cache_get(), do_action(), do_action()
empty($deprecated) && !empty($option) && !isset([…][$option]) && !wp_installing() && $autoload && is_array($notoptions) && isset($notoptions[$option])121–127wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_load_alloptions(), wp_cache_set(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
!empty($deprecated) && !empty($option) && isset([…][$option]) && !$autoload && is_array($notoptions) && isset($notoptions[$option])121–127_deprecated_argument(), wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_cache_set(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
empty($deprecated) && !empty($option) && isset([…][$option]) && $autoload && is_array($notoptions) && isset($notoptions[$option])124–130wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_load_alloptions(), wp_cache_set(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
!empty($deprecated) && !empty($option) && !isset([…][$option]) && !wp_installing() && $autoload && is_array($notoptions) && isset($notoptions[$option])125–131_deprecated_argument(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_load_alloptions(), wp_cache_set(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
!empty($deprecated) && !empty($option) && isset([…][$option]) && $autoload && is_array($notoptions) && isset($notoptions[$option])128–134_deprecated_argument(), wp_installing(), wp_protect_special_option(), sanitize_option(), wp_cache_get(), apply_filters(), get_option(), maybe_serialize(), wp_determine_option_autoload_value(), do_action(), ->prepare(), ->query(), wp_installing(), wp_autoload_values_to_autoload(), wp_load_alloptions(), wp_cache_set(), wp_cache_get(), wp_cache_set(), do_action(), do_action()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev16512–13414
8.516512–13414
8.416512–134145 fewer instructions than PHP 8.3
8.317012–13914
8.217012–13914
8.117012–139142 fewer instructions than PHP 7.4
7.417214–14114

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 4

4 hooks fire while add_option() runs, in this order:

  1. apply_filters( default_option_{$option} )filterline 1115 (+54 into the body)

    Filters the default value for an option.

  2. do_action( add_option )actionline 1132 (+71 into the body)

    Fires before an option is added.

  3. do_action( add_option_{$option} )actionline 1168 (+107 into the body)

    Fires after a specific option has been added.

  4. do_action( added_option )actionline 1178 (+117 into the body)

    Fires after an option has been added.

Uses · 15

Show all 15

Used by · 9

Source code

function add_option( $option, $value = '', $deprecated = '', $autoload = null ) {	global $wpdb; 	if ( ! empty( $deprecated ) ) {		_deprecated_argument( __FUNCTION__, '2.3.0' );	} 	if ( is_scalar( $option ) ) {		$option = trim( $option );	} 	if ( empty( $option ) ) {		return false;	} 	/*	 * Until a proper _deprecated_option() function can be introduced,	 * redirect requests to deprecated keys to the new, correct ones.	 */	$deprecated_keys = array(		'blacklist_keys'    => 'disallowed_keys',		'comment_whitelist' => 'comment_previously_approved',	); 	if ( isset( $deprecated_keys[ $option ] ) && ! wp_installing() ) {		_deprecated_argument(			__FUNCTION__,			'5.5.0',			sprintf(				/* translators: 1: Deprecated option key, 2: New option key. */				__( 'The "%1$s" option key has been renamed to "%2$s".' ),				$option,				$deprecated_keys[ $option ]			)		);		return add_option( $deprecated_keys[ $option ], $value, $deprecated, $autoload );	} 	wp_protect_special_option( $option ); 	if ( is_object( $value ) ) {		$value = clone $value;	} 	$value = sanitize_option( $option, $value ); 	/*	 * Make sure the option doesn't already exist.	 * We can check the 'notoptions' cache before we ask for a DB query.	 */	$notoptions = wp_cache_get( 'notoptions', 'options' ); 	if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) {		/** This filter is documented in wp-includes/option.php */		if ( apply_filters( "default_option_{$option}", false, $option, false ) !== get_option( $option ) ) {			return false;		}	} 	$serialized_value = maybe_serialize( $value ); 	$autoload = wp_determine_option_autoload_value( $option, $value, $serialized_value, $autoload ); 	/**	 * Fires before an option is added.	 *	 * @since 2.9.0	 *	 * @param string $option Name of the option to add.	 * @param mixed  $value  Value of the option.	 */	do_action( 'add_option', $option, $value ); 	$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 ) );	if ( ! $result ) {		return false;	} 	if ( ! wp_installing() ) {		if ( in_array( $autoload, wp_autoload_values_to_autoload(), true ) ) {

Changelog

Introduced in 1.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.7.0
The autoload values 'yes' and 'no' are deprecated.from the docblock
6.6.0
The $autoload parameter's default value was changed to null.from the docblock
1.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/option.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.