logo

$20
How to fix "Undefined Index" on Theme Option Code?

I followed Alex Denning's tutorial on creating theme option pages, but had a few issues. Here is the link to his tutorial:

http://wpshout.com/create-an-advanced-theme-options-page-in-wordpress-day-4

The option page works just fine, but when I turn on wp_debug (in order to submit to the theme WordPress.org) I get this error:

Notice: Undefined index: id in /Users/nathanpbarry/Sites/legend/trunk/wp-content/themes/leather/includes/get-theme-options.php on line 5


and:

Notice: Undefined index: std in /Users/nathanpbarry/Sites/legend/trunk/wp-content/themes/leather/includes/get-theme-options.php on line 6


These are repeated several times. The code in question is here:

<?php
//allows the theme to get info from the theme options page
global $options;
foreach ($options as $value) {
if (get_option( $value['id'] ) === FALSE) {
$$value['id'] = $value['std'];
}
else { $$value['id'] = get_option( $value['id'] ); }
}
?>


How would I get rid of the errors?

nathanbarry | 07/23/10 at 3:25pm | Edit


(3) Possible Answers Submitted...

  • avatar
    Last edited:
    07/23/10
    3:44pm
    Utkarsh Kukreti says:

    Use

    <?php

    //allows the theme to get info from the theme options page

    global $options;

    foreach ($options as $value) {

    if (isset($value['id']) && get_option( $value['id'] ) === FALSE && isset($value['std'])) {

    $$value['id'] = $value['std'];

    }

    elseif (isset($value['id'])) { $$value['id'] = get_option( $value['id'] ); }

    }

    ?>

    Previous versions of this answer: 07/23/10 at 3:41pm

    • 07/23/10 3:39pm

      nathanbarry says:

      That removed several of the errors, but I still have more related to this line:

      else { $$value['id'] = get_option( $value['id'] ); }

    • 07/23/10 3:42pm

      Utkarsh Kukreti says:

      Fixed.

  • avatar
    Last edited:
    07/23/10
    3:35pm
    Oleg Butuzov says:

    <?php
    //allows the theme to get info from the theme options page
    global $options;

    foreach ($options as $value) {
    $$value['id'] = get_option( $value['id'] ) === FALSE && isset($value['id']) ? $value['std'] : get_option( $value['id'] );
    }
    ?>

    Previous versions of this answer: 07/23/10 at 3:35pm

    • 07/23/10 3:36pm

      Oleg Butuzov says:

      just one question why use loop for option to replace variablem in it just by one values (in a case if options empty)...?

  • avatar
    Last edited:
    07/23/10
    3:33pm
    wjm says:

    you should use isset()


    if ( isset( $value['id'] ) && get_option( $value['id'] ) === FALSE) {


    let me know if that works.

    you may also use (in other cases) is_empty()

    • 07/23/10 3:41pm

      wjm says:

      now looking at the code, you will get a second at ELSE level
      enclosing the whole block with "if ( isset( $value['id'] ) ){ ... }" will be safer


      <?php
      //allows the theme to get info from the theme options page
      global $options;

      foreach ($options as $value) {
      if ( isset( $value['id'] ) ) {
      if (get_option( $value['id'] ) === FALSE) {
      $$value['id'] = $value['std'];
      } else {
      $$value['id'] = get_option( $value['id'] );
      }
      }
      }

      ?>



      another way of solving it skipping that loop in foreach with continue


      <?php
      //allows the theme to get info from the theme options page
      global $options;

      foreach ($options as $value) {
      if ( !isset( $value['id'] ) )
      continue;

      if (get_option( $value['id'] ) === FALSE) {
      $$value['id'] = $value['std'];
      } else {
      $$value['id'] = get_option( $value['id'] );
      }
      }
      ?>

This question has expired.





Current status of this question: Completed