WordPress.org

Ready to get started?Download WordPress

Forums

Problem with checked() function (8 posts)

  1. Scott Fennell
    Member
    Posted 2 years ago #

    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

  2. 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 }
  3. Scott Fennell
    Member
    Posted 2 years ago #

    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. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

  4. Scott Fennell
    Member
    Posted 2 years ago #

    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?

  5. Scott Fennell
    Member
    Posted 2 years ago #

    Gaaaaaaaaaahhhhh, here's the paste bin:

    http://pastebin.com/f0urPc8k

  6. 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.

  7. jamesdbruner
    Member
    Posted 1 year ago #

    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.

  8. Scott Fennell
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags