admin管理员组文章数量:1130349
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.
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.
本文标签: optionscheckbox with getoption not working
版权声明:本文标题:options - checkbox with get_option not working 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749169200a2326707.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论