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.
Parameters
$id
stringrequired- Slug-name to identify the field. Used in the
'id'
attribute of tags. $title
stringrequired- Formatted title of the field. Shown as the label for the field during output.
$callback
callablerequired- Function that fills the field with the desired form inputs. The function should echo its output.
$page
stringrequired- The slug-name of the settings page on which to show the section (general, reading, writing, …).
$section
stringoptional- The slug-name of the section of the settings page in which to show the box. Default
'default'
.Default:
'default'
$args
arrayoptional- Extra arguments that get passed to the callback function.
label_for
stringWhen supplied, the setting title will be wrapped in a<label>
element, itsfor
attribute populated with this value.class
stringCSS Class to be added to the<tr>
element when the field is output.
Default:
array()
Source
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,
);
}
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.A checkbox settings field can be checked on the front end by simply looking for isset. No need to add additional checks like 1, 0, true, false…. if a checkbox is not set then it returns false.
Then the callback would be added as such:
And checking to render action on the front side would use:
Optionally you can add a ‘false’ into any conditional (empty, null, ”, 0).
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.
Object Oriented:
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 HTMLid
tag of yourinput
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 correctinput
element with the correctid
tag.The ‘label_for’ element in the $args array should also match the very same
id
in order for the browser to understand whichlabel
belongs to whichinput
field.It worth noting also, that the
id
tag of theinput
element should also match the $option_name (2nd) parameter you are using in yourregister_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']
ofadd_settings_field()
call and the$option_name
ofregister_setting()
call PLUS theid
you use in your input field callback, should all be the same, uniqueid
. Also the sameid
should be used as the$option
parameter in theget_option($option)
calls to get the value of the setting.name="option_name[key]"
. The option_name is the value passed toregister_setting()
(the 2nd parameter). The key sets the key of the key=>value pair in the resulting associative array that is passed to yoursanitize_callback
handler. Even thoughregister_setting()
‘s optional $args parameter can state that your option type is an array, there is no automatic combining or separating of individual key=>value pairs you might be storing within the array. That needs to be done by you in yoursanitize_callback
handler, after youget_option()
, and in theadd_settings_field()
callbacks.