Question

I know this may be a duplicate however I haven't been able to make sense of previous questions.

I have a checkbox on a settings page. Everything is okay on the first load of the page, if I check the box and save all is fine. If I then uncheck i get the following error:

Notice: Undefined index: dat_checkbox_field_0 in .../wp-content/plugins/divi-auto-testimonials/admin/dat-options.php on line 49 value='1'>

The function:

function dat_checkbox_field_0_render(  ) { 

    $options = get_option( 'dat_settings' );
    ?>
    <input type='checkbox' name='dat_settings[dat_checkbox_field_0]' <?php checked( $options['dat_checkbox_field_0'], 1 ); ?> value='1'>
    <?php

}

Line 49 is the input html.

I also get the same error for this code:

$options = get_option( 'dat_settings' );
if( $options['dat_checkbox_field_0'] != '1' ) {
 include_once "admin/notification.php";
}

From what I understand I need to set the value as null I think but I am not entirely sure if that is correct and if so how.

Was it helpful?

Solution

Managed to fix this by doing the following:

function dat_checkbox_field_0_render(  ) { 

    $options = get_option( 'dat_settings' );
    $a = $options;
if (array_key_exists("dat_checkbox_field_0",$a))
  { } else { 
    $options['data_checkbox_field_0'] = false;
  }
    ?>
      <input type='checkbox' name='dat_settings[dat_checkbox_field_0]' <?php checked( $options['dat_checkbox_field_0'], 1 ); ?> value='1'>
    <?php

}

OTHER TIPS

$options = get_option( 'dat_settings' );
$options['dat_checkbox_field_0'] = empty( $options['dat_checkbox_field_0'] ) ? 0 : 1;

Basically, if the variable is "empty", meaning that it is not set, or that it is equal to false (zero qualifies), then it will be assigned the value of 0. If it's set to true or equivalent (one qualifies) then it will be assigned the value of 1.

Alternatively, reverse the logic for the same result (for purists who like true to come first in ternary statements):

$options['dat_checkbox_field_0'] = !empty( $options['dat_checkbox_field_0'] ) ? 1 : 0;

A better approach to answer your question and fix the problem is to make the form actually send a zero value and make WordPress save that key with a zero value in the first place.

I am only posting this here because I was searching with the wrong keywords and only found answers like the ones here that maninuplate the form output to work around the Undefined index: error. But these solutions will still remove the key from the options array. That can cause problems in other places.

Adding another hidden input field will actually make the form save the zero value in the database like this:

<form>
  <input type='hidden' value='0' name='selfdestruct'>
  <input type='checkbox' value='1' name='selfdestruct'>
</form>

Make sure to place the hidden input above your regular one, with the value '0' and the same name as in the regular input field.

That answer is from here: https://stackoverflow.com/a/1992745/4688612

Please send all credit to that poster.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top