admin管理员组

文章数量:1332382

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 ) . ' />';

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 ) . ' />';
Share Improve this question edited Jun 26, 2020 at 12:49 Leo asked Jun 25, 2020 at 10:20 LeoLeo 33 bronze badges 4
  • 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 Commented Jun 25, 2020 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 Commented Jun 25, 2020 at 12:16
  • 1 did you write code to do this? if so please share it – mozboz Commented Jun 25, 2020 at 13:03
  • I added the code to my question – Leo Commented Jul 1, 2020 at 12:02
Add a comment  | 

1 Answer 1

Reset to default 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/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

本文标签: Wordpress theme options checkbox default checked state