功能:
add_option()函数功能是给wp_options表中新建一条数据,用来保存我们需要序列化的数据,有关WordPress选项表请查看:wp_options表介绍,当我们成功添加了一条数据后,就可以使用get_option()函数来获取这条数据,通过update_option()函数来更新这条数据,通过delete_option()函数来删除这条数据。
用法:
参数:
$option
(string) (必选) 需要添加的选项名称
$value
(mixed) (可选) 添加的选项值
默认是空字符串
$deprecated
(string) (可选) 这个参数已经在WordPress 2.3版本废弃了
$autoload
(string) (可选) 指定这个选项是否通过 wp_load_alloptions() 函数自动加载,传入yes 或者 no,默认yes
返回值:
(boolean) 添加选项成功返回True,添加选项失败返回False
所在位置:
add_option() 函数包含在 wp-includes/option.php 中。
源码:
is_day()函数源码since 1.5.0
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;
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();
$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;
}
示例:
相关函数:
get_option()
update_option()
delete_option()
感觉本站内容不错,读后有收获?小额赞助
还可以分享文章给好友: