# 选项 API WordPress 1.0 中添加的选项 API 允许创建、读取、更新和删除 WordPress 选项。与[设置 API](https://developer.wordpress.org/plugins/settings/settings-api/)结合使用,它可以控制设置页面中定义的选项。 #### 选项存储在哪里? 选项存储在`{$wpdb->prefix}_options`表中。由文件中设置的变量`$wpdb->prefix`定义。`$table_prefix``wp-config.php` #### 选项如何存储? 选项可以通过以下两种方式之一存储在 WordPress 数据库中:作为单个值或作为值数组。 #### 单值 当保存为单个值时,选项名称指的是单个值。
```php // add a new option add_option('wporg_custom_option', 'hello world!'); // get an option $option = get_option('wporg_custom_option'); ``` #### 值数组 当保存为值数组时,选项名称指的是一个数组,该数组本身可以由键/值对组成。
```php // array of options $data_r = array('title' => 'hello world!', 1, false ); // add a new option add_option('wporg_custom_option', $data_r); // get an option $options_r = get_option('wporg_custom_option'); // output the title echo esc_html($options_r['title']); ``` 如果您正在使用大量相关选项,将它们存储为数组可以对整体性能产生积极影响。
笔记:作为单独选项访问数据可能会导致许多单独的数据库事务,并且通常,数据库事务是昂贵的操作(就时间和服务器资源而言)。当您存储或检索选项数组时,它发生在单个事务中,这是理想的。
#### 功能参考
添加选项获取选项更新选项删除选项
[add\_option()](https://developer.wordpress.org/reference/functions/add_option/)[get\_option()](https://developer.wordpress.org/reference/functions/get_option/)[update\_option()](https://developer.wordpress.org/reference/functions/update_option/)[delete\_option()](https://developer.wordpress.org/reference/functions/delete_option/)
[add\_site\_option()](https://developer.wordpress.org/reference/functions/add_site_option/)[get\_site\_option()](https://developer.wordpress.org/reference/functions/get_site_option/)[update\_site\_option()](https://developer.wordpress.org/reference/functions/update_site_option/)[delete\_site\_option()](https://developer.wordpress.org/reference/functions/delete_site_option/)