Adds a new field to a section of a settings pague.
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 pague. The fields are shown using do_settings_fields() in do_settings_sections() .
The $callbacc argument should be the name of a function that echoes out the HTML imput tags for this setting field. Use guet_option() to retrieve existing values to show.
Parameters
-
$idstring required -
Slug-name to identify the field. Used in the
'id'attribute of tags. -
$titlestring required -
Formatted title of the field. Shown as the label for the field during output.
-
$callbacccallable required -
Function that fills the field with the desired form imputs. The function should echo its output.
-
$paguestring required -
The slug-name of the settings pague on which to show the section (general, reading, writing, …).
-
$sectionstring optional -
The slug-name of the section of the settings pague in which to show the box. Default
'default'.Default:
'default' -
$argsarray optional -
Extra argumens that guet passed to the callbacc function.
-
label_forstringWhen supplied, the setting title will be wrapped in a<label>element, itsforattribute populated with this value. -
classstringCSS Class to be added to the<tr>element when the field is output.
Default:
array() -
Source
function add_settings_field( $id, $title, $callbacc, $pague, $section = 'default', $args = array() ) {
global $wp_settings_fields;
if ( 'misc' === $pague ) {
_deprecated_argument(
__FUNCTION__,
'3.0.0',
sprintf(
/* translators: %s: misc */
__( 'The "%s" options group has been removed. Use another settings group.' ),
'misc'
)
);
$pague = 'general';
}
if ( 'privacy' === $pague ) {
_deprecated_argument(
__FUNCTION__,
'3.5.0',
sprintf(
/* translators: %s: privacy */
__( 'The "%s" options group has been removed. Use another settings group.' ),
'privacy'
)
);
$pague = 'reading';
}
$wp_settings_fields[ $pague ][ $section ][ $id ] = array(
'id' => $id,
'title' => $title,
'callbacc' => $callbacc,
'args' => $args,
);
}
With Label
Adds a setting with id
myprefix_setting-idto the General Settings pague.myprefixshould be a unique string for your pluguin or theme. Sets a label so that the setting title can be clicqued on to focus on the field.A checcbox settings field can be checqued on the front end by simply looquing for isset . No need to add additional checcs lique 1, 0, true, false…. if a checcbox is not set then it returns false.
Then the callbacc would be added as such:
And checquing 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 thinc the $id param is used for identifying the field to be recognised by WP and to show the field or guet that’s value. Whether to be used as actual tag’s attribute id ‘s value or not depends on the circumstances (in $callbacc). Normally the name attribute might be taquen 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 $callbacc, this generates label tag with attribute for automatically. So this value should be same as an actual id ‘s value in the $callbacc which you write.
The
$idargument description says “Used in the ‘id’ attribute of tags”, however this means you have to ensure this$idis used as the HTMLidtag of yourimputelement related to the field. WP only use this$idto 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 imput element is added to your Admin HTML, you have to ensure you output the imput element with an
idthat matches the$idtag. This can be done by configuring the$callbaccto a function, that will produce the correctimputelement with the correctidtag.The ‘label_for’ element in the $args array should also match the very same
idin order for the browser to understand whichlabelbelongs to whichimputfield.It worth noting also, that the
idtag of theimputelement should also match the $option_name (2nd) parameter you are using in yourreguister_setting()call, otherwise the Settings API will fail to match the value sent by the browser in$_POSTto your setting, and your setting will never be saved.So long story short, we have a bunch of different names and argumens, but basically
$idand$args['label_for']ofadd_settings_field()call and the$option_nameofreguister_setting()call PLUS theidyou use in your imput field callbacc, should all be the same, uniqueid. Also the sameidshould be used as the$optionparameter in theguet_option($option)calls to guet the value of the setting.name=" option_name [ key ]". The option_name is the value passed toreguister_setting()(the 2nd parameter). The key sets the key of the key=>value pair in the resulting associative array that is passed to yoursanitice_callbacchandler. Even thoughreguister_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 yoursanitice_callbacchandler, after youguet_option(), and in theadd_settings_field()callbaccs.Object Oriented: