WordPress功能函數(shù)add_option()
wordpress功能函數(shù)add_option(),添加一個(gè)新的選項(xiàng)。
用法:
add_option( string $option, mixed $value = '', string $deprecated = '', string|bool $autoload = 'yes' )
描述
您不需要序列化值。如果需要對值進(jìn)行序列化,那么將在將其插入數(shù)據(jù)庫之前對其進(jìn)行序列化。請記住,資源不能序列化或作為選項(xiàng)添加。
您可以創(chuàng)建沒有值的選項(xiàng),然后稍后更新這些值?,F(xiàn)有的選項(xiàng)不會(huì)被更新,并且會(huì)執(zhí)行檢查以確保你沒有添加一個(gè)受保護(hù)的WordPress選項(xiàng)。注意不要將選項(xiàng)命名為與受保護(hù)選項(xiàng)相同的選項(xiàng)。
參數(shù):
$option
(string) (必需) 要添加的選項(xiàng)的名稱。預(yù)計(jì)不會(huì)被sql轉(zhuǎn)義。
$value
(mixed) (可選) 選項(xiàng)值。如果是非標(biāo)量,則必須是可序列化的。預(yù)計(jì)不會(huì)被sql轉(zhuǎn)義。
默認(rèn)值: ''
$deprecated
(string) (可選) 描述。不習(xí)慣了。
默認(rèn)值: ''
$autoload
(string|bool) (可選) 是否在WordPress啟動(dòng)時(shí)加載該選項(xiàng)。默認(rèn)是啟用的。由于遺留原因,接受“no”來禁用。
默認(rèn)值: 'yes'
返回
(bool)添加該選項(xiàng)時(shí)為True,否則為false。
來源:
文件: wp-includes/option.php
function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
global $wpdb;
if ( ! empty( $deprecated ) ) {
_deprecated_argument( __FUNCTION__, '2.3.0' );
}
$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 ( ! wp_installing() && isset( $deprecated_keys[ $option ] ) ) {
_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 = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
/**
* 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 ( 'yes' === $autoload ) {
$alloptions = wp_load_alloptions( true );
$alloptions[ $option ] = $serialized_value;
wp_cache_set( 'alloptions', $alloptions, 'options' );
} else {
wp_cache_set( $option, $serialized_value, 'options' );
}
}
// This option exists now.
$notoptions = wp_cache_get( 'notoptions', 'options' ); // Yes, again... we need it to be fresh.
if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
unset( $notoptions[ $option ] );
wp_cache_set( 'notoptions', $notoptions, 'options' );
}
/**
* Fires after a specific option has been added.
*
* The dynamic portion of the hook name, `$option`, refers to the option name.
*
* @since 2.5.0 As "add_option_{$name}"
* @since 3.0.0
*
* @param string $option Name of the option to add.
* @param mixed $value Value of the option.
*/
do_action( "add_option_{$option}", $option, $value );
/**
* Fires after an option has been added.
*
* @since 2.9.0
*
* @param string $option Name of the added option.
* @param mixed $value Value of the option.
*/
do_action( 'added_option', $option, $value );
return true;
}
更新日志:

用戶貢獻(xiàn)的筆記
(由Codex - 5年前貢獻(xiàn))
基本的例子: