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

        '<input type="checkbox" id="%2$s" name="%1$s[%2$s]" '. checked("on", '%3$s').'
         <label for="%2$s">%4$s</label>
         <hr /> ',

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?

  • 1
    checked() prints the value immediately, before even printf() is finished. Look at the third parameter.
    – fuxia
    Commented Nov 21, 2016 at 17:04
  • @toscho And how can I change it if I want that something changes ? I already tried the third param with false and true, but nothing changed. Commented Nov 21, 2016 at 18:18
  • 1
    checked("on", '%3$s', false) is evaluated first, the returned string is passed to printf().
    – fuxia
    Commented Nov 21, 2016 at 18:43

1 Answer 1


Because the order of operations means you're passing that variable into your checked function before the printf function is even being run, which is what's parsing the enumerated variables.

Here's a possible solution haven't tested it though:

    '<input type="checkbox" id="%2$s" name="%1$s[%2$s]" %3$s)'.
    '<label for="%2$s">%4$s</label>' .
    '<hr />',
    checked("on", $args['value'], false ),
  • Ah ok, thank you, but what do I have to change that it works ? Commented Nov 21, 2016 at 18:19
  • Do I have to access the args array directly like args[1]? Is there no other way that I can use %3$s? Commented Nov 21, 2016 at 18:27
  • Did you try running the variable on your variables being passed? I'll edit my answer to reflect what I'm talking about, but I won't be able to test it.
    – socki03
    Commented Nov 21, 2016 at 18:45
  • 1
    I changed the code to pass a third argument (as false) to the checked() function to make sure the result is not echoed but returned instead. Props to @toscho who pointed that out in a comment to the question itself.
    – JHoffmann
    Commented Nov 21, 2016 at 18:55
  • @toscho Ok so I have to put checked("on", '%3$s', false) in the params of the printf method that I can use it like I want? It will not work if I add false in my example at the top ? Thank you to all of you! Commented Nov 21, 2016 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.