0

To understand the Settings API I'm creating a custom settings page for a custom plugin. I have some trouble to save the settings, I have a single checkbox to test if the code works well, but if I check it and then click save button it will not be saved and didn't remain checked. What's wrong with the code? Is possible to use the - caratchers for the ID of a settings field or section?

register_setting(
      'general', // Settings section ID
      'show-countdown' // Settings field ID - maybe the problem?
    );

The code for the checkbox is the following.

<input type="checkbox" class="" id="show-countdown" name="show-countdown" value="1" <?php checked(1, get_option('show-countdown'), false); ?> />
    <label for="show-countdown"><?php echo $args[0]; ?></label>

NB: the register_settings_section() and register_settings_field() are working fine, this is why I omit the code.

  • The code you've included in the question is not sufficient for this to work, so you should include all the relevant code. Remember, it's not working and you need help to find out why, so don't leave out other code assuming it's fine. – Jacob Peattie Jan 14 at 10:35
  • I've checked the database, the field is saved correctly. The problem is with the third parameter of the checked() function that was setted on false. I've removed it and now the checkbox value is showed correctly. Thanks for the help! – sialfa Jan 14 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.