How to use checkbox and radio button in options page?

Call me stupid but I coudn’t figure out how to do it. For text input I would just:

<input type="text" name="option_name"  value="<?php echo get_option( 'option_name' ); ?>" />

and then hook it into workdpress using register_setting(). I could then get its value thru get_option('option_name'). How should I do that with checkboxes and radio buttons?

Related posts

Leave a Reply

2 comments

  1. I tend to store multiple options as an array, so i’d have something like this..

    <?php $options = get_option( 'myoption' ); ?>
    <input type="checkbox" name="myoption[option_one]" value="1"<?php checked( 1 == $options['option_one'] ); ?> />
    <input type="checkbox" name="myoption[option_two]" value="1"<?php checked( 1 == $options['option_two'] ); ?> />
    

    However it does depend how the callback function that sanitizes the incoming data deals with the saved value(the callback you should be defining as the third parameter of register_setting). Personally when i’m dealing with checkboxes I don’t set the array key, where as others may choose to set the key to 0(or whatever instead)…

    So my code actually tends to look like this..

    <?php $options = get_option( 'myoption' ); ?>
    <input type="checkbox" name="myoption[option_one]" value="1"<?php checked( isset( $options['option_one'] ) ); ?> />
    <input type="checkbox" name="myoption[option_two]" value="1"<?php checked( isset( $options['option_two'] ) ); ?> />
    

    If i’m only dealing with checkboxes my sanitization callback will look something along the lines of..

    public function on_option_save( $options ) {
        if( !is_array( $options ) || empty( $options ) || ( false === $options ) )
            return array();
    
        $valid_names = array_keys( $this->defaults );
        $clean_options = array();
    
        foreach( $valid_names as $option_name ) {
            if( isset( $options[$option_name] ) && ( 1 == $options[$option_name] ) )
                $clean_options[$option_name] = 1;
            continue;
        }
        unset( $options );
        return $clean_options;
    }
    

    Ripped that straight from one of my plugin classes(a plugin with only checkbox options), but it’s not code you can expect to work if you copy, it’s there for illustration only..

    For radios, if you’re not using multiple selection it goes something like this..

    <?php $options = get_option( 'my_option' ); ?>
    <input type="radio" name="myoption[option_three]" value="value1"<?php checked( 'value1' == $options['option_three'] ); ?> />
    <input type="radio" name="myoption[option_three]" value="value2"<?php checked( 'value2' == $options['option_three'] ); ?> />
    

    NOTE: It would of course to be wise to check the key is set before comparing against it’s value (i’ve left that out of the above to keep it short).

    Did the above help? If not, just let me know what needs clarifying… (or what i’m missing)..

    RE: checked()

    You can find where the function is defined(in WordPress) here.
    http://core.trac.wordpress.org/browser/tags/3.0.2/wp-includes/general-template.php#L2228

    The first parameter is basically a conditional statement, and the second parameter(if you want to define it) is what to check against. The default value to compare against is TRUE… so if were to do checked( 1 == 1, true ) i’d be checking if 1 == 1 is equal to true. If the conditional hits a match, then you get checked="checked" returned to you..

    NOTE: I’m rubbish at explaining things, so if the above needs further clarification I won’t be offended… just let me know.. 😉

  2. checkbox:

    <input name="option_name" type="checkbox" value="1" <?php checked( '1', get_option( 'option_name' ) ); ?> />
    

    radio:

    <input name="option_name" type="radio" value="0" <?php checked( '0', get_option( 'option_name' ) ); ?> />
    <input name="option_name" type="radio" value="1" <?php checked( '1', get_option( 'option_name' ) ); ?> />