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?

2 Answers 2


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;
    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.. ;)

  • My brain isn't functioning this ime because I am frustrated that I couldn't figure this out on my own. Could you explain what checked( 1 == $options['option_one'] does? Is checked() a php function because I couldn't find it in the manual.
    – Joann
    Dec 7, 2010 at 16:24
  • I can't explain in a comment, i'll update my answer shortly, see above.. ;)
    – t31os
    Dec 7, 2010 at 16:29
  • Ahh! Thanks so much for the help! The checked() function is the only one I couldn't find thru google because apparently it's not documented. I was so used to getting exactly what I want when querying "term + wordpress". :-)
    – Joann
    Dec 7, 2010 at 16:53
  • To clarify, first parameter is what to check, second is what to compare the first value against... so you could do this for example... checked( 1, 2 ) to check if 1 is equal to 2 ... which would output nothing, since this funciton is specificially designed to output a checked state for checkboxes or radio buttons.. in any case, happy to help... ;)
    – t31os
    Dec 7, 2010 at 17:05


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


<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' ) ); ?> />
  • There's a typo in your code(the type)..
    – t31os
    Dec 7, 2010 at 16:20
  • 1
    You gave a direct answer to the question, so you get a +1 from me ... ;)
    – t31os
    Dec 7, 2010 at 17:21
  • 3
    This should be the answer.
    – user26607
    Jun 20, 2013 at 13:28
  • This is it! Solved, should be the answer. Feb 7, 2017 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.