0

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

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
    Nov 19, 2018 at 18:14
  • @Amirition any links to documentation pls? should i replace 1 with on or off?
    – user145078
    Nov 19, 2018 at 18:28
  • 1
    @Amirition the values are being saved as int , checked
    – user145078
    Nov 19, 2018 at 18:48

1 Answer 1

2

Full 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.

5
  • Hi, thanks but i am confused with answer :) . But i think my case is different? i have to loop through checkboxes and other stuffs
    – user145078
    Nov 19, 2018 at 18:26
  • i am taking values from array
    – user145078
    Nov 19, 2018 at 18:32
  • Check your values in database table wp_options. It seems that WordPress saves different values than you check Nov 19, 2018 at 18:35
  • the values are getting saved(1 and 0) but they are of default_values
    – user145078
    Nov 19, 2018 at 18:49
  • 1
    i was able to solve my problem by removing <input type="hidden" name="checkboxoutput[<?php echo (int)$id; ?>][checkbox_check]" value="<?php echo (int)$checkbox_check; ?>" /> ...your answer is best method for saving the checkbox so marking it as answer. i am also using the same method but in different way..i showed only the form part
    – user145078
    Nov 20, 2018 at 0:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.