How to use a newly registered setting with a checkbox and the default value ‘true’

I’m currently working on a plugin in which I would like to have a new setting (called ‘show_introduction’) that has the default value ‘true’.

I did this by the following which is hooked into ‘admin_init’

Read More
register_setting('tf-song-list-settings-group', 'show_introduction');
if (get_option('show_introduction') == '') update_option('show_introduction', 'true');

On the settings page I have the following to incorporate the above setting into a checkbox

<input name="show_introduction" type="checkbox" id="show_introduction" value="true" <?php checked('true', get_option('show_introduction')); ?> />

Now the problem is that I cannot UNcheck the checkbox and thus set the value to false.

What am I doing wrong, and how is it done right?

If I give the setting ‘false’ by default, all is working nicely. But that’s not what I want the default value to be.

Of course, I could rename the setting from ‘show_introduction’ into ‘hide_introduction’, give the input the value ‘false’, check for ‘false’, and hence do it all the other way around. Most probably, that would work. But that’s more like a workaround to the actual problem, isn’t it?

Thanks in advance.

// edit

I think I got what I wanted. Here’s the code

if (!class_exists('tf_song_list')) {
    class tf_song_list {
        public function __construct() {
            add_action('admin_init', array(&$this, 'init_options'));
        } // public function __construct

        function init_options() {
            register_setting('tfsl_options', 'tfsl_options', array(&$this, 'validate_options'));
            $options = get_option('tfsl_options');
            if (false === $options)
                $options = $this->get_default_options();
            update_option('tfsl_options', $options);
        } // function init_options

        function get_default_options() {
            $options = array(
                'show_introduction' => 1
            );
            return $options;
        } // function get_default_options

        function validate_options($options) {
            $validated_options['show_introduction'] = ($option = $options['show_introduction']) ? $option : 0;
            return $validated_options;
        } // function validate_options
    } // class tf_song_list
} // if (!class_exists('tf_song_list'))

The clue, I guess, is
A) to use a validation/sanitizing function to set the specific values in case it is not the default value,
and B) to check with (false === MY_OPTIONS) and only then set default values.

Now, I can use the following on my settings page and all is working fine:

<input name="tfsl_options[show_introduction]" type="checkbox" id="tfsl_options[show_introduction]" value="1" <?php checked(1, $options['show_introduction']); ?> />

Thanks for your input!

BTW, if there’s something wacky with my above code, feel free to enlighten me. I didn’t use the settings API until two days ago.

Related posts

Leave a Reply

2 comments

  1. The get_option function has a second parameter that allows you to specify a default value as noted in the documentation here if I am understanding you correctly.

    Does this do anything?

    <input name="show_introduction" type="checkbox" id="show_introduction" value="true" <?php checked('true', get_option('show_introduction', 'true')); ?> />
    
  2. Since you asked for my solution as an aswer, here it is.

    Firstly, as Chip Bennett pointed out, I had to re-arrange my whole approach to settings and options.

    As of now, I ended up using the following code:

    class MYClass {
        function __construct() {
            add_action('admin_init', array($this, 'register_settings'));
        }
    
        function activate() {
            if (false === get_option('MY_options'))
                update_option('MY_options', $this->get_default_options());
        }
    
        function register_settings() {
            register_setting('MY_options', 'MY_options', array($this, 'validate_options'));
            if (false === ($options = get_option('MY_options')))
                $options = $this->get_default_options();
            update_option('MY_options', $options);
        }
    
        function get_default_options() {
            $options = array(
                'cb_option' => 1, // the checkbox-option in question
                'option_n' => 'whatever',
                ...
            );
            return $options;
        }
    
        function validate_options($options) {
            $validated['cb_option'] = (1 === $options['cb_option']) ? 1 : 0;
            $validated['option_n'] = SANITIZE_IN_SOME_WAY($options['title_name']);
            ...
            return $validated;
        }
    }
    
    $MYClassObject = new MYClass();
    

    On the desired (settings) page, the code snippet for the checkbox is as follows:

    <?php $O = get_option('MY_options'); ?>
    <input name="MY_options[cb_option]" type="checkbox" id="MY_options[cb_option]" value="1" <?php checked(1, $O['cb_opion']); ?> />
    

    If checked, the option is set to 1, and if unchecked, the option is ‘unset’ (i.e. set to ”), what again is ‘validated’ into 0 by means of the validation function. Thus, I can treat the option as boolean, no matter what. The value is either 1 or 0.

    That is the exact behavior I was looking for when I asked the question.