Title: Problem with checked() function
Last modified: August 20, 2016

---

# Problem with checked() function

 *  [Scott Fennell](https://wordpress.org/support/users/scofennellgmailcom/)
 * (@scofennellgmailcom)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/problem-with-checked-function/)
 * Hi,
 * Why does this:
 *     ```
       function sunburst_theme_checkbox_input() {
       	$options = get_option( 'sunburst_theme_options' );
       	?> <input id='checkbox' name='sunburst_theme_options[checkbox]' type='checkbox' value='1' <?php checked( $options['checkbox'], 1  ) ; ?> />
       <?php }
       ```
   
 * …give me this when the form is submitted without the checkbox checked:
 * > Notice: Undefined index: checkbox in /home/scottfen/public_html/wordpress1/
   > wp-content/themes/sunburst/functions.php on line 1388

Viewing 7 replies - 1 through 7 (of 7 total)

 *  [Scott Reilly](https://wordpress.org/support/users/coffee2code/)
 * (@coffee2code)
 * WordPress & Plugin Developer
 * [14 years, 1 month ago](https://wordpress.org/support/topic/problem-with-checked-function/#post-2705042)
 * The theme is likely not saving a value for the ‘checkbox’ setting unless it’s
   checked. If a checkbox isn’t checked, it isn’t included in the submitted form.
   If the plugin uses the submitted form directly (without checking for the presence
   of the ‘checkbox’ field and appropriately setting its value to 0 if it isn’t 
   present), then that setting won’t get saved in the settings array. Thus your 
   use of `$options['checkbox']` is referencing an array element that does not exist.
 * You can account for that by modifying you code snippet as such:
 *     ```
       function sunburst_theme_checkbox_input() {
       	$options = get_option( 'sunburst_theme_options' );
       	if ( ! isset( $options['checkbox'] ) )
       		$options['checkbox'] = 0;
       	?> <input id='checkbox' name='sunburst_theme_options[checkbox]' type='checkbox' value='1' <?php checked( $options['checkbox'], 1  ) ; ?> />
       <?php }
       ```
   
 *  Thread Starter [Scott Fennell](https://wordpress.org/support/users/scofennellgmailcom/)
 * (@scofennellgmailcom)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/problem-with-checked-function/#post-2705046)
 * Thanks very much, that will be a big help. I really appreciate this.
 * But actually I am the theme author here and I want to make sure I am following
   best practices with the settings API. It sounds like I’m doing something wrong
   with the way I’m telling wordpress to register the setting maybe?
 * I like the modification you posted, but I’m not seeing it in other tutorials 
   and books, so I want to know what I’m doing wrong that’s making that extra step
   necessary. Shouldn’t my call to register_setting() save it automatically?
 * Here’s the complete block of code:
 * _[33 lines of code moderated as per the [Forum Rules](http://codex.wordpress.org/Forum_Welcome#Posting_Code).
   The maximum number of lines of code that you can post in these forums is **ten
   lines**. Please use the [pastebin](http://wordpress.pastebin.com/)]_
 *  Thread Starter [Scott Fennell](https://wordpress.org/support/users/scofennellgmailcom/)
 * (@scofennellgmailcom)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/problem-with-checked-function/#post-2705052)
 * Oh… wait a minute… when I call register settings, I could also be calling a callback
   function to sanitize values. Is that the best-practice place to tell WP to save
   the value as 0 if the box is not checked?
 * Any chance you could help me flesh this out a bit?
 *  Thread Starter [Scott Fennell](https://wordpress.org/support/users/scofennellgmailcom/)
 * (@scofennellgmailcom)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/problem-with-checked-function/#post-2705121)
 * Gaaaaaaaaaahhhhh, here’s the paste bin:
 * [http://pastebin.com/f0urPc8k](http://pastebin.com/f0urPc8k)
 *  [Scott Reilly](https://wordpress.org/support/users/coffee2code/)
 * (@coffee2code)
 * WordPress & Plugin Developer
 * [14 years, 1 month ago](https://wordpress.org/support/topic/problem-with-checked-function/#post-2705293)
 * Yes, you’ll want to register a callback to handle sanitization of submitted form
   values. It’s the third argument to the `register_setting()` function.
 * An example of the validation function:
 *     ```
       function sunburst_theme_options_validate( $input ) {
         $options = get_option( 'sunburst_theme_options' );
         if ( ! isset( $input['checkbox'] ) || $input['checkbox'] != '1' )
           $options['checkbox'] = 0;
         else
           $options['checkbox'] = 1;
         return $options;
       }
       ```
   
 * If you have other options in your theme you’ll want to sanitize them in there
   as well.
 *  [jamesdbruner](https://wordpress.org/support/users/jamesdbruner/)
 * (@jamesdbruner)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/problem-with-checked-function/#post-2705494)
 * Scott, would you mind post the code that ended up working for you? I’m having
   a similar issue and I’d love to see an example of what actually works! I’m not
   getting an error message, but I can’t get my checkbox to save and it’s driving
   me up the wall.
 *  Thread Starter [Scott Fennell](https://wordpress.org/support/users/scofennellgmailcom/)
 * (@scofennellgmailcom)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/problem-with-checked-function/#post-2705495)
 * I don’t have the code handy, as I was just building a test theme at the time.
 * But I do remember the root problem: Once I caught on the the practice of using
   a callback function to sanitize the values, I was actually using it to run the
   value through a regular expression that was too strict for my form input. I think
   I was only allowing lowercase letters and the checkbox value was ‘1’ or something.
 * I can’t really remember, I’m sorry.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Problem with checked() function’ is closed to new replies.

## Tags

 * [checked](https://wordpress.org/support/topic-tag/checked/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 7 replies
 * 3 participants
 * Last reply from: [Scott Fennell](https://wordpress.org/support/users/scofennellgmailcom/)
 * Last activity: [13 years, 3 months ago](https://wordpress.org/support/topic/problem-with-checked-function/#post-2705495)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
