I try to safe a checkbox with the Wordpress settings API.
For that I use the checked()
function.
First I make my field with add_settings_field()
and pass a view parameters in an array to the callback function.
In the callback function of my field I make an output with
printf(
'<input type="checkbox" id="%2$s" name="%1$s[%2$s]" '. checked("on", '%3$s').'
<label for="%2$s">%4$s</label>
<hr /> ',
$args['option_name'],
$args['name'],
$args['value'],
$args['description']
);
Now normally the checkbox should be saved. BUT the check-function doesn't work.
When I print the parameter value %3$s
then the value is "on" if the field is checked and nothing if the field is not checked.
But the check-function never triggers even if %3$s
is "on".
Only if I insert the "on" string checked("on", 'on')
directly it returns checked.
So my question is: Why I can't use %3$s
in the checked function?
checked()
prints the value immediately, before evenprintf()
is finished. Look at the third parameter.checked("on", '%3$s', false)
is evaluated first, the returned string is passed toprintf()
.