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

Undefined index errors in theme options page WordPress

  • SOLVED

Hey. I'm working on a WordPress theme and when I turn WP_DEBUG on, the error message gives me an "undefined index id" and "undefined index std" when saving or resetting the options. The offending code is in this function (the bolded parts are the offending lines given in the error message):

function cpress_add_admin() {
global $themename, $shortname, $options;
$settings = get_option($shortname.'_options');
if ( isset($_GET['page']) && $_GET['page'] == basename(__FILE__) ) {
if ( isset($_POST['action']) && $_POST['action'] == 'export') {
cpress_export();
}
if (isset($_FILES['settings'])){
if ($_FILES["settings"]["error"] > 0){
header("Location: themes.php?page=options_page.php&nofile=true");
}else{
$filename = basename($_FILES['settings']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext == "dat") &&
($_FILES["settings"]["size"] < 1000000)) {
$rawdata = file_get_contents($_FILES["settings"]["tmp_name"]);
$cp_options = unserialize($rawdata);
if ($cp_options !== false) {
update_option('cpress_options', $cp_options);
header("Location: themes.php?page=options_page.php&import=true");
} else {header("Location: themes.php?page=options_page.php&datawrong=true");}
} else {header("Location: themes.php?page=options_page.php&filewrong=true");}
} /*else */
} /*isset*/

//saving options
if ( isset($_POST['action']) && $_POST['action'] == 'save' ) {
foreach ($options as $value) {

<strong> if( !isset( $_REQUEST[$value['id']] ) ) {$settings[$value['id']] = '';
continue; }</strong>

if( $value['type'] === "checkbox") {
// No isset check here, the top conditional catches non-set items
if( is_array( $_REQUEST[$value['id']] ) )
$_REQUEST[$value['id']] = implode( ',', $_REQUEST[$value['id']] );
}
$settings[$value['id']] = $_REQUEST[$value['id']];
}

update_option($shortname.'_options', $settings);
header("Location: themes.php?page=options_page.php&saved=true");
die;

//reset options
} else if(isset($_POST['action']) && 'reset' == $_POST['action'] ) {
foreach ($options as $value) {
<strong>$key = $value['id'];
$std = $value['std'];
$new_options[$key] = $std;</strong>
}
update_option($shortname.'_options', $new_options );
header("Location: themes.php?page=options_page.php&reset=true");
die;
}
}
add_theme_page($themename." Options", "Citizen Press Options", 'edit_themes', basename(__FILE__), 'cpress_admin');
}


The specific bits that's causing the error is this (around line 32):

if( !isset( $_REQUEST[$value['id']] ) ) {$settings[$value['id']] = '';
continue; }


and this (around lines 51-52):

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


The options page works perfectly fine, but in debug mode these notices show up. Any insights would be GREATLY appreciated! Thanks. :)

Answers (3)

2010-12-20

Michael Fields answers:

if( !isset( $_REQUEST[$value['id']] ) ) {$settings[$value['id']] = '';

continue; }


You need to check if the id index of the value variable is set before using it. I would not nest array selectors like this either.

Something like this may work a bit better for you:
$key = ( isset( $value['id'] ) ) ? $value['id'] : '';
if ( ! isset( $_REQUEST[$key] ) ) {
$settings[$key] = '';
continue;
}


Michael Fields comments:

The second part is best represented IMHO as:
$key = 'default-value';
if ( isset( $value['id'] ) ) {
$key = $value['id'];
}

$std = 'default-value';
if ( isset( $value['std'] ) ) {
$std = $value['std'];
}

2010-12-20

Chris Bavota answers:

Sounds to me like you might have some options that do not have an "id" set. Therefore, when it is looping through the variable $value["id"] is coming up empty and causing the indexing error.

You can surround that block with this:

if(!empty($value['id'])) {
// your code
}

Then the code won't run if the variable is empty and your shouldn't get the indexing error.

2010-12-20

Buzu B answers:

try with this for the first line:


$key = isset($value['id']) ? $value['id'] : 'defaultID';
$std = isset($value['std']) ? $value['std'] : 'defaultSTD';


change the defaults according to your preferences.