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.

/* ----------------------------------
 * wordpress之魂 ©
 * ---------------------------------- */
 add_settings_field( 'myprefix_setting-id', 'This is the setting title', 'myprefix_setting_callback_function', 'general', 'myprefix_settings-section-name', array( 'label_for' => 'myprefix_setting-id' ) );

add_settings_field() 示例


添加于 版本: 2.7.0

add_settings_field() 历史


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

/* ----------------------------------
 * wordpress之魂 ©
 * ---------------------------------- */
 * Add a new field to a section of a settings page
 * Part of the Settings API. Use this to define a settings field that will show
 * as part of a settings section inside a settings page. The fields are shown using
 * do_settings_fields() in do_settings-sections()
 * The $callback argument should be the name of a function that echoes out the
 * html input tags for this setting field. Use get_option() to retrieve existing
 * values to show.
 * @since 2.7.0
 * @since 4.2.0 The `$class` argument was added.
 * @global $wp_settings_fields Storage array of settings fields and info about their pages/sections
 * @param string $id       Slug-name to identify the field. Used in the 'id' attribute of tags.
 * @param string $title    Formatted title of the field. Shown as the label for the field
 *                         during output.
 * @param string $callback Function that fills the field with the desired form inputs. The
 *                         function should echo its output.
 * @param string $page     The slug-name of the settings page on which to show the section
 *                         (general, reading, writing, ...).
 * @param string $section  Optional. The slug-name of the section of the settings page
 *                         in which to show the box. Default 'default'.
 * @param array  $args {
 *     Optional. Extra arguments used when outputting the field.
 *     @type string $label_for When supplied, the setting title will be wrapped
 *                             in a `

add_settings_field() 源文件