Adds 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.
$idstringrequired
'id' attribute of tags.$titlestringrequired
$callbackcallablerequired
$pagestringrequired
$sectionstringoptional
'default'.Default:'default'
$argsarrayoptional
label_for string<label> element, its for attribute populated with this value.class string<tr> element when the field is output.Default:array()
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.
function add_settings_field( $id, $title, $callback, $page, $section = 'default', $args = array() ) {
global $wp_settings_fields;
if ( 'misc' === $page ) {
_deprecated_argument(
__FUNCTION__,
'3.0.0',
sprintf(
/* translators: %s: misc */
__( 'The "%s" options group has been removed. Use another settings group.' ),
'misc'
)
);
$page = 'general';
}
if ( 'privacy' === $page ) {
_deprecated_argument(
__FUNCTION__,
'3.5.0',
sprintf(
/* translators: %s: privacy */
__( 'The "%s" options group has been removed. Use another settings group.' ),
'privacy'
)
);
$page = 'reading';
}
$wp_settings_fields[ $page ][ $section ][ $id ] = array(
'id' => $id,
'title' => $title,
'callback' => $callback,
'args' => $args,
);
}
© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/add_settings_field