Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to fix "Undefined Index" on Theme Option Code? WordPress

  • SOLVED

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

[[LINK href="http://wpshout.com/create-an-advanced-theme-options-page-in-wordpress-day-4"]]http://wpshout.com/create-an-advanced-theme-options-page-in-wordpress-day-4[[/LINK]]

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?

Answers (3)

2010-07-23

Utkarsh Kukreti answers:

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'] ); }

}

?>


nathanbarry comments:

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

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


Utkarsh Kukreti comments:

Fixed.

2010-07-23

Oleg Butuzov answers:

<?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'] );
}
?>


Oleg Butuzov comments:

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

2010-07-23

wjm answers:

you should use <strong>isset()</strong>


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

let me know if that works.

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


wjm comments:

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 <strong>continue</strong>


<?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'] );
}
}
?>