kirki icon indicating copy to clipboard operation
kirki copied to clipboard

Checkbox default state not saving to DB

Open boquiabierto opened this issue 11 years ago • 4 comments

Checkbox default values are not saved to DB. Only those values are saved that have been manipulated through the interface (checked or unchecked ones). Tested on 1.0.0-beta1 and 1.0.2.

Example: I have 3 fields with the settings-slugs 'one', 'two', 'three', beeing the default value of 'three' = '1' and beeing it in the interface correctly pre-checked (and it’s value beeing 1): when I check “one” through the interface the value saved to the DB should be at least:

{
    one => 1,
    three => 1
}

if not:

{
    one => 1,
    two => 0,
    three = 1
}

but instead you get:

{
    one => 1
}

If I further check and then un-check “two” you get:

{
    one => 1
    two => 0
}

Thanks for looking into it!

boquiabierto avatar Aug 27 '15 09:08 boquiabierto

The example shows obviously the case saving the values all to one option array.

boquiabierto avatar Aug 27 '15 09:08 boquiabierto

I'm afraid I don't quite understand what you mean... Default states are NOT saved in the db. EVER. Not just on Kirki but in the WordPress customizer in general!

aristath avatar Sep 01 '15 01:09 aristath

Oh, well knowing that this is the expected behaviour, I can deal with it, thanks!

boquiabierto avatar Sep 01 '15 12:09 boquiabierto

Yeah, sorry I should have explained this better in the docs I guess. The customizer only saves settings that are modified. That's why the get_option and get_theme_mod functions both have a "default value" argument. So if you're using options then in order to properly get the value you should not do this:

<?php $value = get_option( 'my_setting' ); ?>

but instead you should do this:

<?php $value = get_option( 'my_setting', 'default_value' ); ?>

Same thing goes if you're using theme_mods:

<?php
// BAD
$value = get_theme_mod( 'my_setting' );
// GOOD
$value = get_theme_mod( 'my_setting, 'default_value' );
?>

aristath avatar Sep 01 '15 12:09 aristath