add_settings_field() WordPress Function

The add_settings_field() function is used to add a new settings field to a settings page. This function takes three arguments: the field ID, the field title, and the callback function that will be used to display the field. The field ID is a unique identifier for the field. The field title is the text that will be displayed to the user next to the field. The callback function is used to display the field to the user. This function takes two arguments: the field ID and an array of arguments. The add_settings_field() function can be used to add text fields, select boxes, checkboxes, and radio buttons.

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

Adds a new field to a section of a settings page.


Description

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.


Top ↑

Parameters

$id

(string)(Required)Slug-name to identify the field. Used in the 'id' attribute of tags.

$title

(string)(Required)Formatted title of the field. Shown as the label for the field during output.

$callback

(callable)(Required)Function that fills the field with the desired form inputs. The function should echo its output.

$page

(string)(Required)The slug-name of the settings page on which to show the section (general, reading, writing, ...).

$section

(string)(Optional) The slug-name of the section of the settings page in which to show the box.

Default value: 'default'

$args

(array)(Optional)Extra arguments used when outputting the field.

  • 'label_for'
    (string) When supplied, the setting title will be wrapped in a <label> element, its for attribute populated with this value.
  • 'class'
    (string) CSS Class to be added to the <tr> element when the field is output.

Default value: array()


Top ↑

More Information

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.


Top ↑

Source

File: wp-admin/includes/template.php

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,
	);
}


Top ↑

Changelog

Changelog
VersionDescription
4.2.0The $class argument was added.
2.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.