$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>options - checkbox with get_option not working|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

options - checkbox with get_option not working

matteradmin5PV0评论

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
Add a comment  | 

1 Answer 1

Reset to default 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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far