最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Settings page - can't change checkbox to unchecked

programmeradmin1浏览0评论

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' )      
    );
}

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' )      
    );
}
Share Improve this question edited May 12, 2019 at 17:49 fuxia 107k39 gold badges255 silver badges459 bronze badges asked May 12, 2019 at 16:40 Ryszard JędraszykRyszard Jędraszyk 3244 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I've done some debugging, it came out that this line caused wrong behavior:

$settings = wp_parse_args( $settings, $defaults );

I wondered why it's after this line which supposedly does the same:

$settings = get_option( 'acau_settings', $defaults );

These 2 lines one after another were part of code from a wiser man, so I thought that maybe it's some kind of fallback. It looks like only the latter works correctly for checkboxes.

发布评论

评论列表(0)

  1. 暂无评论