I am trying to figure how to have check box implemented in options page . i am able to get the default values but not save and retrieve it . i am using get_option()
and checked()
functions
<form method="post" action="options.php" id="theme-options-form">
<?php settings_fields('wppl-settings-group-new');?>
<?php do_settings_sections('wppl-settings-group-new');?>
<?php
$default_values = array(
0 => array(
'id' => '0',
'checkbox_check' => '1',
),
1 => array(
'id' => '1',
'checkbox_check' => '0',
));
$checkboxoutput = get_option('saved_value', $default_values );
foreach ($checkboxoutput as $value_new) {
if (isset($value_new['checkbox_check'])) {
$checkbox_check = $value_new['checkbox_check'];
}
else{
$checkbox_check = $checkboxoutput[$id]['checkbox_check']; //if not saved use the datefrom defualt ->ID passed
}
?>
<li class="sortable-item flexit">
<input type="checkbox" class="form-check-input" id="exampleCheck1" name="checkboxoutput[<?php echo (int)$id; ?>][checkbox_check]" value="1"<?php checked('1' , $checkbox_check); ?>
/>
<input type="hidden" name="checkboxoutput[<?php echo (int)$id; ?>][checkbox_check]" value="<?php echo (int)$checkbox_check; ?>" />
</li>
<?php submit_button();?>
</form>
It is getting the default values but not getting saved? please help
I am trying to figure how to have check box implemented in options page . i am able to get the default values but not save and retrieve it . i am using get_option()
and checked()
functions
<form method="post" action="options.php" id="theme-options-form">
<?php settings_fields('wppl-settings-group-new');?>
<?php do_settings_sections('wppl-settings-group-new');?>
<?php
$default_values = array(
0 => array(
'id' => '0',
'checkbox_check' => '1',
),
1 => array(
'id' => '1',
'checkbox_check' => '0',
));
$checkboxoutput = get_option('saved_value', $default_values );
foreach ($checkboxoutput as $value_new) {
if (isset($value_new['checkbox_check'])) {
$checkbox_check = $value_new['checkbox_check'];
}
else{
$checkbox_check = $checkboxoutput[$id]['checkbox_check']; //if not saved use the datefrom defualt ->ID passed
}
?>
<li class="sortable-item flexit">
<input type="checkbox" class="form-check-input" id="exampleCheck1" name="checkboxoutput[<?php echo (int)$id; ?>][checkbox_check]" value="1"<?php checked('1' , $checkbox_check); ?>
/>
<input type="hidden" name="checkboxoutput[<?php echo (int)$id; ?>][checkbox_check]" value="<?php echo (int)$checkbox_check; ?>" />
</li>
<?php submit_button();?>
</form>
It is getting the default values but not getting saved? please help
Share Improve this question edited Nov 19, 2018 at 19:12 asked Nov 19, 2018 at 17:08 user145078user145078 3- 1 Notice that checkboxes saves as "on" or "off" in the database, so your conditional logic may fail when you check it as 0/1. – Amirition Commented Nov 19, 2018 at 18:14
- @Amirition any links to documentation pls? should i replace 1 with on or off? – user145078 Commented Nov 19, 2018 at 18:28
- 1 @Amirition the values are being saved as int , checked – user145078 Commented Nov 19, 2018 at 18:48
1 Answer
Reset to default 2Full code to add settings page with checkbox option. You don't need loops and something else. WordPress will make everything for you. Get more info in Codex
function wpse_319648_render_popup() {
$options = get_option('wpse_319648_checkbox');
$default = isset($options['popup']) ? $options['popup'] : 0;
printf(
'<input type="checkbox" name="%1$s[popup]" value="1" %2$s>',
'wpse_319648_checkbox',
checked($default, 1, false)
);
}
function wpse_319648_settings_page() {
echo '<form class="wrap" action="options.php" method="post">';
settings_fields('wpse-319648-settings');
do_settings_sections('wpse-319648-settings');
submit_button();
echo '</form>';
}
add_action('admin_init', function() {
register_setting('wpse-319648-settings', 'wpse_319648_checkbox');
add_settings_section(
'wpse-319648-section',
__('Settings', 'theme'),
[],
'wpse-319648-settings'
);
add_settings_field(
'popup',
__('Show popup', 'theme'),
'wpse_319648_render_popup',
'wpse-319648-settings',
'wpse-319648-section'
);
});
add_action('admin_menu', function() {
add_submenu_page('options-general.php',
__('Options', 'theme'),
__('Options', 'theme'),
'manage_options',
'my-page',
'wpse_319648_settings_page'
);
});
Hope it helps.