返回介绍

add_option()

发布于 2017-09-10 21:07:04 字数 6461 浏览 1343 评论 0 收藏 0

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

Add 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, re源代码s can not 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.


参数

$option

(string) (Required) Name of option to add. Expected to not be SQL-escaped.

$value

(mixed) (Optional) Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.

Default value: ''

$deprecated

(string) (Optional) description. Not used anymore.

Default value: ''

$autoload

(string|bool) (Optional) Whether to load the option when WordPress starts up. Default is enabled. Accepts 'no' to disable for legacy reasons.

Default value: 'yes'


返回值

(bool) False if option was not added and true if option was added.


源代码

File: 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;

	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;
}

更新日志

Versiondescription
1.0.0Introduced.

相关函数

Uses

  • wp-includes/load.php: wp_installing()
  • wp-includes/cache.php: wp_cache_get()
  • wp-includes/cache.php: wp_cache_set()
  • wp-includes/formatting.php: sanitize_option()
  • wp-includes/functions.php: _deprecated_argument()
  • wp-includes/functions.php: maybe_serialize()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/plugin.php: do_action()
  • wp-includes/option.php: wp_load_alloptions()
  • wp-includes/option.php: add_option
  • wp-includes/option.php: add_option_{$option}
  • wp-includes/option.php: added_option
  • wp-includes/option.php: wp_protect_special_option()
  • wp-includes/option.php: get_option()
  • wp-includes/option.php: default_option_{$option}
  • wp-includes/wp-db.php: wpdb::query()
  • wp-includes/wp-db.php: wpdb::prepare()
  • Show 12 more uses Hide more uses

Used By

  • wp-includes/option.php: add_network_option()
  • wp-includes/theme.php: switch_theme()
  • wp-includes/option.php: set_transient()
  • wp-includes/option.php: update_option()
  • wp-includes/ms-blogs.php: add_blog_option()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Basic Example

    
    <?php add_option( 'myhack_extraction_length', '255', '', 'yes' ); ?>
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文