```php
function wporg_settings_init() {
// register a new setting for "reading" page
register_setting('reading', 'wporg_setting_name');
// register a new section in the "reading" page
add_settings_section(
'wporg_settings_section',
'WPOrg Settings Section', 'wporg_settings_section_callback',
'reading'
);
// register a new field in the "wporg_settings_section" section, inside the "reading" page
add_settings_field(
'wporg_settings_field',
'WPOrg Setting', 'wporg_settings_field_callback',
'reading',
'wporg_settings_section'
);
}
/**
* register wporg_settings_init to the admin_init action hook
*/
add_action('admin_init', 'wporg_settings_init');
/**
* callback functions
*/
// section content cb
function wporg_settings_section_callback() {
echo '
WPOrg Section Introduction.
';
}
// field content cb
function wporg_settings_field_callback() {
// get the value of the setting we've registered with register_setting()
$setting = get_option('wporg_setting_name');
// output the field
?>
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']);
```
如果您正在使用大量相关选项,将它们存储为数组可以对整体性能产生积极影响。
通过遵循注释,可以使用下面的示例来快速参考这些主题。
#### 完整示例
完整的示例添加了一个名为 的顶级菜单`WPOrg`,注册了一个名为 的自定义选项`wporg_options`,并使用设置 API 和选项 API(包括显示错误/更新消息)执行 CRUD(创建、读取、更新、删除)逻辑。
```php
/**
* @internal never define functions inside callbacks.
* these functions could be run multiple times; this would result in a fatal error.
*/
/**
* custom option and settings
*/
function wporg_settings_init() {
// Register a new setting for "wporg" page.
register_setting( 'wporg', 'wporg_options' );
// Register a new section in the "wporg" page.
add_settings_section(
'wporg_section_developers',
__( 'The Matrix has you.', 'wporg' ), 'wporg_section_developers_callback',
'wporg'
);
// Register a new field in the "wporg_section_developers" section, inside the "wporg" page.
add_settings_field(
'wporg_field_pill', // As of WP 4.6 this value is used only internally.
// Use $args' label_for to populate the id inside the callback.
__( 'Pill', 'wporg' ),
'wporg_field_pill_cb',
'wporg',
'wporg_section_developers',
array(
'label_for' => 'wporg_field_pill',
'class' => 'wporg_row',
'wporg_custom_data' => 'custom',
)
);
}
/**
* Register our wporg_settings_init to the admin_init action hook.
*/
add_action( 'admin_init', 'wporg_settings_init' );
/**
* Custom option and settings:
* - callback functions
*/
/**
* Developers section callback function.
*
* @param array $args The settings array, defining title, id, callback.
*/
function wporg_section_developers_callback( $args ) {
?>
```
```php
/**
* @internal never define functions inside callbacks.
* these functions could be run multiple times; this would result in a fatal error.
*/
/**
* custom option and settings
*/
function wporg_settings_init() {
// Register a new setting for "wporg" page.
register_setting( 'wporg', 'wporg_options' );
// Register a new section in the "wporg" page.
add_settings_section(
'wporg_section_developers',
__( 'The Matrix has you.', 'wporg' ), 'wporg_section_developers_callback',
'wporg'
);
// Register a new field in the "wporg_section_developers" section, inside the "wporg" page.
add_settings_field(
'wporg_field_pill', // As of WP 4.6 this value is used only internally.
// Use $args' label_for to populate the id inside the callback.
__( 'Pill', 'wporg' ),
'wporg_field_pill_cb',
'wporg',
'wporg_section_developers',
array(
'label_for' => 'wporg_field_pill',
'class' => 'wporg_row',
'wporg_custom_data' => 'custom',
)
);
}
/**
* Register our wporg_settings_init to the admin_init action hook.
*/
add_action( 'admin_init', 'wporg_settings_init' );
/**
* Custom option and settings:
* - callback functions
*/
/**
* Developers section callback function.
*
* @param array $args The settings array, defining title, id, callback.
*/
function wporg_section_developers_callback( $args ) {
?>
.
* - the "class" key value is used for the "class" attribute of the
containing the field.
* Note: you can add custom key value pairs to be used inside your callbacks.
*
* @param array $args
*/
function wporg_field_pill_cb( $args ) {
// Get the value of the setting we've registered with register_setting()
$options = get_option( 'wporg_options' );
?>