Register a settings field to a settings page and section.

This is part of the Settings API, which lets you automatically generate wp-admin settings pages by registering your settings and using a few callbacks to control the output.

This function assumes you already know the settings $page and the page $section that the field should be shown on.

You MUST register any options used by this function with register_setting() or they won't be saved and updated automatically.

The callback function needs to output the appropriate html input and fill it with the old value, the saving will be done behind the scenes.

The html input field's name attribute must match $option_name in register_setting(), and value can be filled using get_option().

This function can also be used to add extra settings fields to the default WP settings pages like media or general. You can add them to an existing section, or use add_settings_section() to create a new section to add the fields to.

See Settings API for details.

add_settings_field() 描述


<?php add_settings_field$id$title$callback$page$section$args ); ?>

add_settings_field() 用法



(string) (必填) String for use in the 'id' attribute of tags.

默认值: None


(string) (必填) Title of the field.

默认值: None


(string) (必填) Function that fills the field with the desired inputs as part of the larger form. Passed a single argument, the $args array. Name and id of the input should match the $id given to this function. The function should echo its output.

默认值: None


(string) (必填) The menu page on which to display this field. Should match $menu_slug from add_theme_page() or from do_settings_sections().

默认值: None


(string) (可选) The section of the settings page in which to show the box (default or a section you added with add_settings_section(), look at the page in the source to see what the existing ones are.)

默认值: default


(array) (可选) Additional arguments that are passed to the $callback function. The 'label_for' key/value pair can be used to format the field title like so: <label for="value">$title</label>.

默认值: array()

add_settings_field() 参数



This function does not return a value.

add_settings_field() 返回值


With Label

Adds a setting with id "myprefix_setting-id" to the General Settings page. "myprefix" should be a unique string for your plugin or theme. Sets a label so that the setting title can be clicked on to focus on the field.

add_settings_field() 示例


添加于 版本: 2.7.0

add_settings_field() 历史


add_settings_field() 函数的代码位于 wp-admin/includes/template.php.

add_settings_field() 源文件