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

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


Description 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.


Parameters 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 ↑

Source 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

Changelog
Version Description
4.2.0 The $class argument was added.
2.7.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    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( 'myprefix_setting-id',
    	'This is the setting title',
     	'myprefix_setting_callback_function',
    	'general',
    	'myprefix_settings-section-name',
    	array( 'label_for' => 'myprefix_setting-id' ) );
    
    
  2. Skip to note 2 content
    Contributed by princepink

    I suspect Used in the ‘id’ attribute of tags might be rewritten to Used in the ‘name’ attribute of tags.
    I think the $id param is used for identifying the field to be recognised by WP and to show the field or get that’s value. Whether to be used as actual tag’s attribute id‘s value or not depends on the circumstances (in $callback). Normally the name attribute might be taken for this aim.
    I just had been confused this param means to generate the attribute for some form element, but it seems not. When put ‘label_for’ in the $args that will be passed to the $callback, this generates label tag with attribute for automatically. So this value should be same as an actual id‘s value in the $callback which you write.

  3. Skip to note 3 content
    Contributed by tehlivi
    add_action('admin_init', 'your_function');
    function your_function(){
    	add_settings_field(
    		'myprefix_setting-id',
    		'This is the setting title',
    		'myprefix_setting_callback_function',
    		'general',
    		'default',
    		array( 'label_for' => 'myprefix_setting-id' )
    	);
    }
    
    function myprefix_setting_callback_function($args){
    	echo 'Content here';
    }
  4. Skip to note 4 content
    Contributed by Bence Szalai

    The $id argument description says “Used in the ‘id’ attribute of tags”, however this means you have to ensure this $id is used as the HTML id tag of your input element related to the field. WP only use this $id to have an unique key for your field in it’s internal settings_field list ($wp_settings_fields).
    As WP does not control the way the input element is added to your Admin HTML, you have to ensure you output the input element with an id that matches the $id tag. This can be done by configuring the $callback to a function, that will produce the correct input element with the correct id tag.
    The ‘label_for’ element in the $args array should also match the very same id in order for the browser to understand which label belongs to which input field.

    It worth noting also, that the id tag of the input element should also match the $option_name (2nd) parameter you are using in your register_setting() call, otherwise the Settings API will fail to match the value sent by the browser in $_POST to your setting, and your setting will never be saved.

    So long story short, we have a bunch of different names and arguments, but basically $id and $args['label_for'] of add_settings_field() call and the $option_name of register_setting() call PLUS the id you use in your input field callback, should all be the same, unique id. Also the same id should be used as the $option parameter in the get_option($option) calls to get the value of the setting.

    register_setting( 'mygroup', 'mynewcheckboxID' );
    add_settings_field(
    		'mynewcheckboxID', 
    		'My New Checkbox',
    		'callback_input_myid',
    		'myAdminPage',
    		'myAdminSection', 
    		[
    			'label_for' => 'mynewcheckboxID'
    		] );
    
    function callback_input_myid() {
    	echo "<input type='checkbox' id='mynewcheckboxID' value='1'"
    	if ( get_option('mynewcheckboxID') == '1' ) {
    		echo ' checked';
    	}
     	echo '/>';
    }
    
  5. Skip to note 5 content
    Contributed by tehlivi

    Object Oriented:

    class ClassName {
    	public function __construct() {
    		add_action( 'admin_init', array( $this, 'your_function' ) );
    	}
    
    	function your_function() {
    		add_settings_field(
    			'myprefix_setting-id',
    			'This is the setting title',
    			array( $this, 'myprefix_setting_callback_function' ),
    			'general',
    			'default',
    			array( 'label_for' => 'myprefix_setting-id' ),
    		);
    	}
    
    	function myprefix_setting_callback_function( $args ) {
    		echo 'Content here';
    	}
    }
    
    $ClassName = new ClassName();
    

You must log in before being able to contribute a note or feedback.