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:1070
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, ornullto 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
- Scaling
- Constant
- Instructions
- 12–134
- Plugin surface
- 4 hooks
- Called by
- 9
Reaches the database via ->query().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 165.
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.
9 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
add_option()this function does it - cacheobject cache
wp_cache_get()called directly - hookthird-party callbacks
apply_filters()called directly - serializeserialisation
maybe_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.
| When | Instructions | Calls it makes |
|---|---|---|
empty($deprecated) && empty($option) | 12–14 | none |
!empty($deprecated) && empty($option) | 16–18 | _deprecated_argument() |
empty($deprecated) && !empty($option) && isset([…][$option]) && !wp_installing() | 40–42 | wp_installing(), __(), sprintf(), _deprecated_argument(), add_option() |
empty($deprecated) && !empty($option) && !isset([…][$option]) | 44–50 | wp_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–53 | wp_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–69 | 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]) | 68–72 | wp_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–81 | 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() | 78–84 | 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]) | 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–92 | 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]) | 89–95 | 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() | 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–98 | 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() | 96–104 | 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() && !$autoload | 96–102 | 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]) | 97–101 | 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() | 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–107 | 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]) && !$autoload | 99–105 | 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() | 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() && !$autoload | 100–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() && $autoload | 103–109 | 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() | 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]) && !$autoload | 103–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–110 | 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() && !$autoload | 104–108 | 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() && !$autoload | 106–114 | 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]) && $autoload | 106–112 | 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]) && wp_installing() && is_array($notoptions) && isset($notoptions[$option]) | 107–113 | 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]) && !$autoload | 107–111 | 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]) && is_array($notoptions) && isset($notoptions[$option]) && !wp_installing() && $autoload | 107–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() && !$autoload | 108–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]) && !$autoload | 109–117 | 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 | 110–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]) && $autoload | 110–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() && $autoload | 111–115 | 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]) && 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]) && !$autoload | 111–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() && $autoload | 113–121 | 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 | 113–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–120 | 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]) && $autoload | 114–118 | 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]) && is_array($notoptions) && isset($notoptions[$option]) && !wp_installing() && $autoload | 115–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]) && $autoload | 116–124 | 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]) && !$autoload && is_array($notoptions) && isset($notoptions[$option]) | 117–123 | 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]) && !wp_installing() && $autoload | 117–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]) && $autoload | 118–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]) && $autoload | 120–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–127 | 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]) | 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–130 | 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() |
!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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 165 | 12–134 | 14 | |
| 8.5 | 165 | 12–134 | 14 | |
| 8.4 | 165 | 12–134 | 14 | 5 fewer instructions than PHP 8.3 |
| 8.3 | 170 | 12–139 | 14 | |
| 8.2 | 170 | 12–139 | 14 | |
| 8.1 | 170 | 12–139 | 14 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 172 | 14–141 | 14 |
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:
- apply_filters( default_option_{$option} )filterline 1124 (+54 into the body)
Filters the default value for an option.
- do_action( add_option_{$option} )actionline 1177 (+107 into the body)
Fires after a specific option has been added.
Uses · 15
- _deprecated_argument()Marks a function argument as deprecated and inform when it has been used.
- wp_installing()Checks or sets whether WordPress is in "installation" mode.
- __()Retrieves the translation of $text.
- add_option()Adds a new option.
- wp_protect_special_option()Protects WordPress special option from being modified.
- sanitize_option()Sanitizes various option values based on the nature of the option.
- wp_cache_get()Retrieves the cache contents from the cache by key and group.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_option()Retrieves an option value based on an option name.
- maybe_serialize()Serializes data, if needed.
- wp_determine_option_autoload_value()Determines the appropriate autoload value for an option based on input.
- do_action()Calls the callback functions that have been added to an action hook.
Show all 15
- wp_autoload_values_to_autoload()Returns the values that trigger autoloading from the options table.
- wp_load_alloptions()Loads and caches all autoloaded options, if available or all options.
- wp_cache_set()Saves the data to the cache.
Used by · 9
- add_blog_option()Adds a new option for a given blog ID.
- add_network_option()Adds a new network option.
- add_option()Adds a new option.
- set_transient()Sets/updates the value of a transient.
- switch_theme()Switches the theme.
- update_option()Updates the value of an option that was already added.
- upgrade_110()Execute changes made in WordPress 1.2.
- upgrade_340()Execute changes made in WordPress 3.4.
- upgrade_630()Executes changes made in WordPress 6.3.0.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.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.