WordPress函数文档add_option()

向wp_option表添加选项/值对 描述 译文 这是一种向选项数据库表中添加有名称的选项/值对的安全方法。如…

向wp_option表添加选项/值对

描述

译文

这是一种向选项数据库表中添加有名称的选项/值对的安全方法。如果所需选项已存在,add_option()不添加内容。选项被保存后,可通过get_option()来访问选项,通过update_option()来修改选项,还可以通过delete_option()删除该选项。

在INSERT语句前,add_option的值被$wpdb->escape 跳过。

WP 2.3系列版本或更高版本的用法

WP 2.3系列中,$description参数已经停用,wp_option表中也删除了该参数的值。用法相同,但不再使用第二个参数。

该函数在早期版本中的用法见下文

WP 2.3系列版本或更高版本中该函数的示例

WP 2.3系列版本或更高版本中该函数的参数

$name

(字符串)(必需)需要添加的选项名称。用下划线隔开单词;不要使用大写字母——参数值会被存入数据库

默认值:None

$value

(字符串)(可选)当前选项名称的值。限制在2到32个字节内

默认值:Empty

$deprecated

(字符串)(可选)不再使用

默认值:Empty

$autoload

(字符串)(可选)当前选项是否需要被wp_load_alloptions函数自动加载(加载每个页面时将选项放在对象缓存中)?有效值:yes或no。

默认值:yes

$description停用前该函数的用法

原文

A safe way of adding a named option/value pair to the options database table. It does nothing if the option already exists. After the option is saved, it can be accessed with get_option(), changed with update_option(), and deleted with delete_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. You can create options without values and then add values later.

Calling add_option first checks whether the option has already been added, and returns false if an option with the same name exists. Next, it checks to make sure the option name is not one of the protected names alloptions or notoptions, and dies with an error message if attempting to overwrite a protected option. If the option name is not protected, and does not already exist, the option will be created.

Note: add_option uses get_option to determine whether the option already exists, and since get_option returns false as the default value, if you set an option to false in the database (e.g. via update_option($option_name, false)), then a subsequent call to add_option will change the value, because it will seem to add_option that the option does not exist.

vs. update_option()

If you are trying to ensure that a given option is created, use update_option() instead, which bypasses the option name check and updates the option with the desired value whether or not it exists.

NB: you cannot specify autoload=’no’ if you use update_option(). If you need to specify autoload=’no’, and you are not sure whether the option already exists, then call delete_option() first before calling add_option().

用法

<?php add_option$option$value$deprecated$autoload ); ?>

参数

$option

(string) (必填) Name of the option to be added. Must not exceed 64 characters. Use underscores to separate words, and do not use uppercase—this is going to be placed into the database.

默认值: None

$value

(mixed) (可选) Value for this option name. Limited to 2^32 bytes of data

默认值: Empty string

$deprecated

(string) (可选) Deprecated in WordPress Version 2.3.

默认值: Empty string

$autoload

(string) (可选) Should this option be automatically loaded by the function wp_load_alloptions() (puts options into object cache on each page load)? Valid values: yes or no.

默认值: yes

历史

添加于 版本: 1.0.0

源文件

add_option() 函数的代码位于 wp-includes/option.php.

相关

  • Options API
  • add_option()
  • add_blog_option()
  • add_site_option()
  • delete_option()
  • delete_blog_option()
  • delete_site_option()
  • form_option()
  • get_option()
  • get_blog_option()
  • get_site_option()
  • get_site_url()
  • get_user_option()
  • update_option()
  • update_blog_option()
  • update_site_option()
  • update_user_option()
  • wp_load_alloptions()
  • 原文:http://codex.wordpress.org/Function_Reference/add_option
类别:WordPress函数文档

本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

评论 (0)COMMENT