I thought that I got my plugin settings finally working with defaults, without overriding db with them, but it's only the case for acau_update_delay
which is numeric value and not for the checkboxes. Both checkboxes are checked by default and unfortunately when I uncheck them and save settings, they are checked again.
I've even researched why I can't get it to work, but couldn't find way to solve it with my implementation. The problem is that only checked checkboxes are saved on database. So if they are unchecked, there will be no db entry, for example options['acau_cart_notices_off']
will be missing, instead of what would be convenient have 0 value.
Therefore my function acau_get_settings
will override these missing settings values with defaults = 1 for each checkbox, instead of leaving them alone.
How can I fix it?
public function acau_get_settings( $index = false ) {
$defaults = array ( 'acau_update_delay' => 1000,'acau_positive_qty' => 1,'acau_cart_notices_off' => 1 );
$settings = get_option( 'acau_settings', $defaults );
$settings = wp_parse_args( $settings, $defaults );
if ( $index && isset( $settings[ $index ] ) ) {
return $settings[ $index ];
}
return $settings;
}
I start creation of settings page like this, calling function acau_get_settings
:
public function create_settings_page()
{
$this->options = $this->acau_get_settings();
Here is callback for one of checkbox settings:
public function acau_cart_notices_off_callback() {
printf(
'<fieldset>
<label><input id="acau_cart_notices_off" type="checkbox" name="acau_settings[acau_cart_notices_off]" value="1" %1$s />%2$s</label>
</fieldset>',
isset( $this->options['acau_cart_notices_off'] ) && ( 1 == $this->options['acau_cart_notices_off'] ) ? 'checked="checked" ':'',
__( 'Turn off notices on cart page. Most common are "Cart updated." and notice about product removed from cart.', 'ajax-cart-autoupdate' )
);
}