0

I am developing a custom theme. I added a checkbox with the settings api. They all work except for the ones that are checked by default (checked="checked"). They do not save when they get unchecked (state only, value does save).

Now, there are two things I'd like to be able to do: 1 - save the checkbox state when unchecked on default checked checkboxes. 2 - save default checked checkboxes automatically without the user first having to click "Save changes". (so on page load save default checked checkboxes for example).

I searched everywhere for an answer already but I could not find anyone with this same question. They all handle checkboxes but none handle default "checked" checkboxes.

The code for the checkboxes:

echo '<input type="checkbox" class="theme-custom-checkbox" name="theme_custom_option" value="1" checked="checked"' . checked( 1, get_option( 'theme_custom_option' ), false ) . ' />';
  • 1 - there's no reason the default state of a checkbox would cause it to not be saved if other checkboxes in the same form are being saved ok. You may want to post your code for this (both the code that makes the form and what the resulting HTML looks like) because perhaps you have a bug there. – mozboz Jun 25 at 10:36
  • sorry, I do understand your confusion and I apologize. The checkbox values do save but the states of only the default checkboxes turn back to their checked state after saving. So I wonder how I should make this work. – Leo Jun 25 at 12:16
  • 1
    did you write code to do this? if so please share it – mozboz Jun 25 at 13:03
  • I added the code to my question – Leo Jul 1 at 12:02
0

With the code that you've added to your question, you're adding the HTML 'checked' attribute twice, which is probably why things are getting confused. You have it hard-coded as checked="checked", but then checked( 1, get_option( 'theme_custom_option' ), false ) will write another checked attribute corresponding to the actual value of the option.

You should be able to verify this would have been able to debug it by looking at the generated HTML with view-source or developer tools.

Taken from the docs page for the checked function in Wordpress https://developer.wordpress.org/reference/functions/checked/ your code should probably look something like this:

echo '<input type="checkbox" class="theme-custom-checkbox" name="theme_custom_option"  value="1" ' ;
echo checked( 1, get_option( 'theme_custom_option' ), false );
echo ' />';

Note code tidied a bit for readability

| improve this answer | |
  • Ofcourse, I was thinking of something like that. I'm going to try your solution right away. – Leo Jul 6 at 14:41
  • It worked! Also is there a way to save the inputs as soon as the page loads, what I mean is when the user installs the theme for the first time, the default checked checkboxes should be saved right away. How can I achieve this? – Leo Jul 6 at 15:06
  • Glad it worked! Yep, you could probably do this with a hook for when the theme gets installed. This is probably worth searching for and open a new question if you can't find it. Upvotes and accepting the answer appreciated if it works for you :) – mozboz Jul 6 at 15:35
  • One thing, when I echo the checked function it worked on the old ones that had the hard coded checked state (so the ones that didnt work at first, but that were saved to the database earlier) , but when I add new checkboxes with default checked state they dont appear checked but unchecked. Is that because the code is being echo'ed after it has been saved? – Leo Jul 6 at 16:33
  • If you're not sure what the state in the database is, then it's probably not a good idea to make the UI checkbox be default checked. Much better to always have the checkbox reflect the state of what's in the database, and if you want that to be default 'on' then find a way to set it at some point, e.g. as you said when theme is installed – mozboz Jul 6 at 16:41

Your Answer

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

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