admin管理员组文章数量:1427538
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
1 Answer
Reset to default 0I'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.
本文标签: Settings pagecan39t change checkbox to unchecked
版权声明:本文标题:Settings page - can't change checkbox to unchecked 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745500916a2661026.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论