Rob Myrick
Rob Myrick

Reputation: 859

Undefined Index Error in Wordpress

I have this code in Wordpress, with undefined index error for $option_name. The options are saving correctly but I'm getting the error.

I have it wrapped with isset so I'm not understanding the problem.

if (isset ($_POST['update_theme_options'])) {

  $option_name_array = array_keys($slider_options);

  foreach ($option_name_array as $option_name):
    if(isset($slider_options[$option_name])):
      $slider_options[$option_name] = $_POST[$option_name];   
    endif;
  endforeach;

  update_option('slider_settings', $slider_options);
}`

Upvotes: 0

Views: 65

Answers (1)

Barmar
Barmar

Reputation: 781503

You're testing the wrong array with isset. $slider_options[$option_name] will always exist, since $option_name came from array_keys($slider_options). I think you meant to do:

if (isset($_POST[$option_name])):

The full code should be:

if (isset ($_POST['update_theme_options'])) {

  $option_name_array = array_keys($slider_options);

  foreach ($option_name_array as $option_name):
    if (isset($_POST[$option_name])):
      $slider_options[$option_name] = $_POST[$option_name];   
    endif;
  endforeach;

  update_option('slider_settings', $slider_options);
}

Upvotes: 1

Related Questions