default_option_{$option}
Filter HookDescription
Adds a new option. 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. / 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.Hook Information
File Location |
wp-includes/option.php
View on GitHub
|
Hook Type | Filter |
Line Number | 1124 |
Hook Parameters
Type | Name | Description |
---|---|---|
string
|
$option
|
Name of the option to add. Expected to not be SQL-escaped. |
mixed
|
$value
|
Optional. Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped. |
string
|
$deprecated
|
Optional. Description. Not used anymore. |
bool|null
|
$autoload
|
Optional. 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. |
Usage Examples
Basic Usage
<?php
// Hook into default_option_{$option}
add_filter('default_option_{$option}', 'my_custom_filter', 10, 4);
function my_custom_filter($option, $value, $deprecated, $autoload) {
// Your custom filtering logic here
return $option;
}
Source Code Context
wp-includes/option.php:1124
- How this hook is used in WordPress core
<?php
1119 */
1120 $notoptions = wp_cache_get( 'notoptions', 'options' );
1121
1122 if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) {
1123 /** This filter is documented in wp-includes/option.php */
1124 if ( apply_filters( "default_option_{$option}", false, $option, false ) !== get_option( $option ) ) {
1125 return false;
1126 }
1127 }
1128
1129 $serialized_value = maybe_serialize( $value );
PHP Documentation
<?php
/**
* Adds a new option.
*
* 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.
*
* @since 1.0.0
* @since 6.6.0 The $autoload parameter's default value was changed to null.
* @since 6.7.0 The autoload values 'yes' and 'no' are deprecated.
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $option Name of the option to add. Expected to not be SQL-escaped.
* @param mixed $value Optional. Option value. Must be serializable if non-scalar.
* Expected to not be SQL-escaped.
* @param string $deprecated Optional. Description. Not used anymore.
* @param bool|null $autoload Optional. 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.
* @return bool True if the option was added, false otherwise.
*/
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.
*/
Quick Info
- Hook Type: Filter
- Parameters: 4
- File: wp-includes/option.php
Related Hooks
Related hooks will be displayed here in future updates.