插件开发文档

使用设置 API

💡 云策文档标注

概述

本文档介绍了如何使用 WordPress 设置 API 来添加和管理设置,包括注册设置、添加部分和字段,以及获取设置值。这些操作通常通过 admin_init 钩子执行,以在管理界面中集成自定义设置。

关键要点

  • 使用 register_setting() 函数定义新设置,它会在 {$wpdb->prefix}_options 表中创建条目。
  • 使用 add_settings_section() 在现有设置页面上添加新部分,用于分组设置。
  • 使用 add_settings_field() 在现有部分中添加新字段,用于具体设置项。
  • 这些函数应添加到 admin_init 动作钩子中以确保正确初始化。
  • 使用 get_option() 函数获取已注册的设置值,接受选项名称和可选默认值参数。

代码示例

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 '<p>WPOrg Section Introduction.</p>';
}

// 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
    ?>
    <input type="text" name="wporg_setting_name" value="<?php echo isset( $setting ) ? esc_attr( $setting ) : ''; ?>">
    <?php
}

📄 原文内容

Adding Settings

You must define a new setting using register_setting() , it will create an entry in the {$wpdb->prefix}_options table.

You can add new sections on existing pages using add_settings_section() .

You can add new fields to existing sections using add_settings_field() .

register_setting() as well as the mentioned add_settings_*() functions should all be added to the admin_init action hook.

Add a Setting

register_setting(<br>    string $option_group,<br>    string $option_name,<br>    array $args = []<br>);

Please refer to the Function Reference about register_setting() for full explanation about the used parameters.

Add a Section

add_settings_section(<br>    string $id,<br>    string $title,<br>    callable $callback,<br>    string $page,<br>    array $args = []<br>);

Sections are the groups of settings you see on WordPress settings pages with a shared heading. In your plugin you can add new sections to existing settings pages rather than creating a whole new page. This makes your plugin simpler to maintain and creates fewer new pages for users to learn.

Please refer to the Function Reference about add_settings_section() for full explanation about the used parameters.

Add a Field

add_settings_field(
    string $id,
    string $title,
    callable $callback,
    string $page,
    string $section = 'default',
    array $args = []
);

Please refer to the Function Reference about add_settings_field() for full explanation about the used parameters.

Example

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 '<p>WPOrg Section Introduction.</p>';
}

// 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
	?>
	<input type="text" name="wporg_setting_name" value="<?php echo isset( $setting ) ? esc_attr( $setting ) : ''; ?>">
    <?php
}

Getting Settings

get_option(
    string $option,
    mixed $default = false
);

Getting settings is accomplished with the get_option() function.
The function accepts two parameters: the name of the option and an optional default value for that option.

Example

// Get the value of the setting we've registered with register_setting()
$setting = get_option('wporg_setting_name');